[gaim-migrate @ 3063]
[pidgin-git.git] / src / protocols / icq / filesession.c
blob09e8fb9faf67a00e0bd7b30b7eb863c0c686b26a
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 /*
4 * $Id: filesession.c 2509 2001-10-13 00:06:18Z warmenhoven $
6 * Copyright (C) 1998-2001, Denis V. Dmitrienko <denis@null.net> and
7 * Bill Soudan <soudan@kde.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <stdlib.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
29 #ifdef _MSVC_
30 #include <io.h>
31 #define open _open
32 #define close _close
33 #define read _read
34 #define write _write
35 #endif
37 #include "icqlib.h"
38 #include "filesession.h"
39 #include "stdpackets.h"
40 #include "socketmanager.h"
42 icq_FileSession *icq_FileSessionNew(icq_Link *icqlink)
44 icq_FileSession *p=(icq_FileSession *)malloc(sizeof(icq_FileSession));
46 if (p)
48 p->status=0;
49 p->id=0L;
50 p->icqlink=icqlink;
51 p->tcplink=NULL;
52 p->current_fd=-1;
53 p->current_file_num=0;
54 p->current_file_progress=0;
55 p->current_file_size=0;
56 p->files=0L;
57 p->current_speed=100;
58 p->total_bytes=0;
59 p->total_files=0;
60 p->total_transferred_bytes=0;
61 p->working_dir[0]=0;
62 p->user_data=NULL;
63 icq_ListInsert(icqlink->d->icq_FileSessions, 0, p);
66 return p;
69 void icq_FileSessionDelete(void *pv)
71 icq_FileSession *p=(icq_FileSession *)pv;
73 invoke_callback(p->icqlink, icq_FileNotify)(p, FILE_NOTIFY_CLOSE, 0,
74 NULL);
76 if(p->files) {
77 char **p2=p->files;
78 while(*p2)
79 free(*(p2++));
80 free(p->files);
81 p->files=NULL;
84 if (p->current_fd > -1 ) {
85 close(p->current_fd);
86 p->current_fd=-1;
89 free(p);
92 int _icq_FindFileSession(void *p, va_list data)
94 icq_FileSession *psession=(icq_FileSession *)p;
95 DWORD uin=va_arg(data, DWORD);
96 unsigned long id=va_arg(data, unsigned long);
98 return (psession->remote_uin == uin) && ( id ? (psession->id == id) : 1 );
102 icq_FileSession *icq_FindFileSession(icq_Link *icqlink, DWORD uin,
103 unsigned long id)
105 return icq_ListTraverse(icqlink->d->icq_FileSessions, _icq_FindFileSession,
106 uin, id);
109 void icq_FileSessionSetStatus(icq_FileSession *p, int status)
111 if(status!=p->status)
113 p->status=status;
114 if(p->id)
115 invoke_callback(p->icqlink, icq_FileNotify)(p, FILE_NOTIFY_STATUS,
116 status, NULL);
117 if (status == FILE_STATUS_SENDING)
118 icq_SocketSetHandler(p->tcplink->socket, ICQ_SOCKET_WRITE,
119 (icq_SocketHandler)icq_FileSessionSendData, p);
120 else
121 icq_SocketSetHandler(p->tcplink->socket, ICQ_SOCKET_WRITE, NULL, NULL);
125 void icq_FileSessionSetHandle(icq_FileSession *p, const char *handle)
127 strncpy(p->remote_handle, handle, 64);
130 void icq_FileSessionSetCurrentFile(icq_FileSession *p, const char *filename)
132 #ifdef _WIN32
133 struct _stat file_status;
134 #else
135 struct stat file_status;
136 #endif
137 char file[1024];
139 strcpy(file, p->working_dir);
140 strcat(file, filename);
142 if (p->current_fd>-1) {
143 close(p->current_fd);
144 p->current_fd=-1;
147 strncpy(p->current_file, file, 64);
148 p->current_file_progress=0;
150 /* does the file already exist? */
151 #ifdef _WIN32
152 if (_stat(file, &file_status)==0) {
153 #else
154 if (stat(file, &file_status)==0) {
155 #endif
156 p->current_file_progress=file_status.st_size;
157 p->total_transferred_bytes+=file_status.st_size;
158 #ifdef _WIN32
159 p->current_fd=open(file, _O_WRONLY | _O_APPEND | _O_BINARY);
160 #else
161 p->current_fd=open(file, O_WRONLY | O_APPEND);
162 #endif
163 } else {
164 #ifdef _WIN32
165 p->current_fd=open(file, _O_WRONLY | _O_CREAT | _O_BINARY,
166 _S_IREAD|_S_IWRITE);
167 #else
168 p->current_fd=open(file, O_WRONLY | O_CREAT, S_IRWXU);
169 #endif
172 /* make sure we have a valid filehandle */
173 if (p->current_fd == -1)
174 perror("couldn't open file: ");
178 void icq_FileSessionPrepareNextFile(icq_FileSession *p)
180 int i=0;
181 char **files=p->files;
183 p->current_file_num++;
185 while(*files) {
186 i++;
187 if(i==p->current_file_num)
188 break;
189 else
190 files++;
193 if(*files) {
194 #ifdef _WIN32
195 struct _stat file_status;
196 #else
197 struct stat file_status;
198 #endif
200 if (p->current_fd>-1) {
201 close(p->current_fd);
202 p->current_fd=-1;
205 #ifdef _WIN32
206 if (_stat(*files, &file_status)==0) {
207 char *basename=*files;
208 char *pos=strrchr(basename, '\\');
209 #else
210 if (stat(*files, &file_status)==0) {
211 char *basename=*files;
212 char *pos=strrchr(basename, '/');
213 #endif
214 if(pos) basename=pos+1;
215 strncpy(p->current_file, basename, 64);
216 p->current_file_progress=0;
217 p->current_file_size=file_status.st_size;
218 #ifdef _WIN32
219 p->current_fd=open(*files, _O_RDONLY | _O_BINARY);
220 #else
221 p->current_fd=open(*files, O_RDONLY);
222 #endif
225 /* make sure we have a valid filehandle */
226 if (p->current_fd == -1)
227 perror("couldn't open file: ");
231 void icq_FileSessionSendData(icq_FileSession *p)
233 /* for now just send a packet at a time */
234 char buffer[2048];
235 int count=read(p->current_fd, buffer, 2048);
237 if(count>0) {
238 icq_Packet *p2=icq_TCPCreateFile06Packet(count, buffer);
239 icq_TCPLinkSend(p->tcplink, p2);
240 p->total_transferred_bytes+=count;
241 p->current_file_progress+=count;
242 icq_FileSessionSetStatus(p, FILE_STATUS_SENDING);
244 invoke_callback(p->icqlink, icq_FileNotify)(p, FILE_NOTIFY_DATAPACKET,
245 count, buffer);
248 /* done transmitting if read returns less that 2048 bytes */
249 if(count<2048)
250 icq_FileSessionClose(p);
252 return;
255 /* public */
257 void icq_FileSessionSetSpeed(icq_FileSession *p, int speed)
259 icq_Packet *packet=icq_TCPCreateFile05Packet(speed);
261 icq_TCPLinkSend(p->tcplink, packet);
264 void icq_FileSessionClose(icq_FileSession *p)
266 icq_TCPLink *plink=p->tcplink;
268 /* TODO: handle closing already unallocated filesession? */
270 /* if we're attached to a tcplink, unattach so the link doesn't try
271 * to close us, and then close the tcplink */
272 if (plink)
274 plink->session=0L;
275 icq_TCPLinkClose(plink);
278 icq_ListRemove(p->icqlink->d->icq_FileSessions, p);
279 icq_FileSessionDelete(p);
282 void icq_FileSessionSetWorkingDir(icq_FileSession *p, const char *dir)
284 int length = sizeof(p->working_dir);
285 strncpy(p->working_dir, dir, length);
286 p->working_dir[length-1]='\0';
289 void icq_FileSessionSetFiles(icq_FileSession *p, char **files)
291 p->files=files;