~ubuntu-branches/ubuntu/hoary/psi/hoary

« back to all changes in this revision

Viewing changes to src/filetrans.h

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2004-06-15 00:10:41 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040615001041-enywb6pcpe4sjsw6
Tags: 0.9.2-1
* New upstream release
* Set KDEDIR for ./configure so kde specific files get installed
* Don't install libpsiwidgets.so. It got installed in /usr/share
  where it doesn't belong. May be included (at a better location)
  later.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
** filetrans.h - classes for handling file transfers
3
 
** Copyright (C) 2001, 2002  Justin Karneges
4
 
**
5
 
** This program is free software; you can redistribute it and/or
6
 
** modify it under the terms of the GNU General Public License
7
 
** as published by the Free Software Foundation; either version 2
8
 
** of the License, or (at your option) any later version.
9
 
**
10
 
** This program is distributed in the hope that it will be useful,
11
 
** but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
** GNU General Public License for more details.
14
 
**
15
 
** You should have received a copy of the GNU General Public License
16
 
** along with this program; if not, write to the Free Software
17
 
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 
**
19
 
****************************************************************************/
20
 
 
21
 
#ifndef FILETRANS_H
22
 
#define FILETRANS_H
23
 
 
24
 
#include<qobject.h>
25
 
#include<qsocket.h>
26
 
#include<qserversocket.h>
27
 
#include<qptrlist.h>
28
 
#include<qguardedptr.h>
29
 
#include<qfileinfo.h>
30
 
#include<qfile.h>
31
 
#include<qsignalmapper.h>
32
 
#include<qcstring.h>
33
 
 
34
 
 
35
 
#define FT_FILE   0
36
 
#define FT_STRING 1
37
 
 
38
 
/****************************************************************************
39
 
  FileServer
40
 
 
41
 
  The FileServer class implements a simple httpd which serves files based
42
 
  on keys (for security).  Use 'addFile' to offer a file.  'addFile'
43
 
  will generate a unique key for the file and return a pointer to a
44
 
  FileServerItem.
45
 
 
46
 
    FileServer ftserv;
47
 
    ftserv.setServer("127.0.0.1", 12345);
48
 
    FileServerItem *item = ftserv.addFile("/home/justin/myfile.txt");
49
 
 
50
 
  The resulting URL (item->url) would be something like:
51
 
 
52
 
    http://127.0.0.1:12345/e1f13de17df17c5a/myfile.txt
53
 
 
54
 
  The local path to the file is kept hidden from the recipient.
55
 
 
56
 
****************************************************************************/
57
 
 
58
 
class FileTransfer;
59
 
struct FileServerItem;
60
 
class ServSock;
61
 
 
62
 
class FileServer : public QObject
63
 
{
64
 
        Q_OBJECT
65
 
public:
66
 
        FileServer();
67
 
        ~FileServer();
68
 
 
69
 
        void setServer(const QString &, int);
70
 
        FileServerItem *addFile(const QString &fname, const QString &desc, const QString &who);
71
 
        QPtrList<FileServerItem> items() const;
72
 
 
73
 
signals:
74
 
        void newDownload(QGuardedPtr<FileTransfer>);
75
 
 
76
 
private slots:
77
 
        void connectionReady(int sock);
78
 
        void transferDiscarded(int id);
79
 
 
80
 
private:
81
 
        FileServerItem *getFSI(const QString &key);
82
 
        void listen();
83
 
        QString genKey();
84
 
 
85
 
        FileTransfer *findByID(int);
86
 
 
87
 
        QSignalMapper *sigmap[2];
88
 
        QPtrList<FileTransfer> ftlist;
89
 
        QPtrList<FileServerItem> filelist;
90
 
        int current_id;
91
 
 
92
 
        ServSock *serv;
93
 
        QString host;
94
 
        int port;
95
 
        void (*old_sigpipe)(int);
96
 
 
97
 
        friend class FileTransfer;
98
 
};
99
 
 
100
 
 
101
 
class ServSock : public QServerSocket
102
 
{
103
 
        Q_OBJECT
104
 
public:
105
 
        ServSock(int port);
106
 
 
107
 
signals:
108
 
        void connectionReady(int);
109
 
 
110
 
protected:
111
 
        // reimplemented
112
 
        void newConnection(int);
113
 
};
114
 
 
115
 
 
116
 
struct FileServerItem
117
 
{
118
 
        int type;
119
 
        QFileInfo file;
120
 
        QString key;
121
 
 
122
 
        QString url, who, desc;
123
 
};
124
 
 
125
 
 
126
 
class FileTransfer : public QObject
127
 
{
128
 
        Q_OBJECT
129
 
public:
130
 
        FileTransfer(FileServer *par);
131
 
        ~FileTransfer();
132
 
 
133
 
        void start();
134
 
        bool isValid() { return v_isValid; }
135
 
 
136
 
        QSocket sock;
137
 
        int id;
138
 
        QFile file;
139
 
        bool v_isValid;
140
 
        bool finished;
141
 
        int type;
142
 
        QByteArray stringData;
143
 
 
144
 
signals:
145
 
        void done();
146
 
        void valid();
147
 
        void discarding();
148
 
 
149
 
private slots:
150
 
        void sock_readyRead();
151
 
        void sock_connectionClosed();
152
 
        void sock_bytesWritten(int);
153
 
        void processChunk();
154
 
 
155
 
private:
156
 
        bool getGETInfo(const QString &getReq, QString *resource, QString *http_ver);
157
 
        bool extractResourceInfo(const QString &resource, QString *fname, QString *key);
158
 
        void discard();
159
 
 
160
 
        FileServer *par;
161
 
        int bytesSent;
162
 
 
163
 
        int at;
164
 
        char recvBuf[1024];
165
 
};
166
 
 
167
 
#endif