summaryrefslogtreecommitdiffstats
path: root/tdefile-plugins/torrent/tdefile_torrent.cpp
blob: 658a7097d4595da5f75376e9efd25f07ccdf033c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
 * Copyright (c) 2003, 2004 2004 Michael Pyne <michael.pyne@kdemail.net>
 *
 * This software is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; see the file COPYING.
 * If not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include <tqdatetime.h>
#include <tqfile.h>
#include <tqregexp.h>
#include <tqdir.h>
#include <tqstringlist.h>

#include <tdemessagebox.h>
#include <kgenericfactory.h>

#include "tdefile_torrent.h"
#include "bdict.h"
#include "blist.h"
#include "bint.h"
#include "bstring.h"

typedef KGenericFactory<KTorrentPlugin> TorrentFactory;
K_EXPORT_COMPONENT_FACTORY(tdefile_torrent, TorrentFactory("tdefile_torrent"))

TQStringList filesList (BList *list);
TQ_ULLONG filesLength (BList *list);

KTorrentPlugin::KTorrentPlugin (TQObject *parent, const char *name,
                                const TQStringList &args)
    : KFilePlugin (parent, name, args), m_failed(true), m_dict(0)
{
    KFileMimeTypeInfo *info = addMimeTypeInfo ("application/x-bittorrent");
    if (!info)
    {
        kdError() << "Error creating application/x-bittorrent mime type info!\n";
        return;
    }

    KFileMimeTypeInfo::GroupInfo* group =
        addGroupInfo (info, "TorrentInfo", i18n("Torrent Information"));
    if (!group)
    {
        kdError() << "Error creating TorrentInfo group!\n";
        return;
    }
    setAttributes (group, KFileMimeTypeInfo::Modifiable);
    
    KFileMimeTypeInfo::ItemInfo *item = 0;
    
    item = addItemInfo(group, "name", i18n("Name"), TQVariant::String);
    if (!item)
    {
        kdError() << "Error adding Name to group!\n";
        return;
    }
    setHint (item, KFileMimeTypeInfo::Name);
    setAttributes (item, KFileMimeTypeInfo::Modifiable);
    
    item = addItemInfo(group, "length", i18n("Torrent Length"), TQVariant::ULongLong);
    if (!item)
    {
        kdError() << "Error adding Length to group!\n";
        return;
    }
    setHint (item, KFileMimeTypeInfo::Length);
    setUnit (item, KFileMimeTypeInfo::Bytes);
    
    item = addItemInfo(group, "announce", i18n("Tracker URL"), TQVariant::String);
    if (!item)
    {
        kdError() << "Error adding Announce to group!\n";
        return;
    }
    
    item = addItemInfo(group, "creation date", i18n("Date Created"), TQVariant::DateTime);
    if (!item)
    {
        kdError() << "Error adding DateCreated to group!\n";
        return;
    }
    
    item = addItemInfo(group, "NumFiles", i18n("Number of Files"), TQVariant::Int);
    if (!item)
    {
        kdError() << "Error adding NumFiles to group!\n";
        return;
    }
    
    item = addItemInfo(group, "piece length", i18n("File Piece Length"), TQVariant::Int);
    if (!item)
    {
        kdError() << "Error adding PieceLength to group!\n";
        return;
    }
    setUnit (item, KFileMimeTypeInfo::Bytes);

    item = addItemInfo(group, "comment", i18n("Comment"), TQVariant::String);
    if (!item)
    {
        kdError() << "Error adding Comment to group!\n";
        return;
    }
    setAttributes (item, KFileMimeTypeInfo::MultiLine | KFileMimeTypeInfo::Modifiable);

    m_failed = false;
}

bool KTorrentPlugin::readInfo (KFileMetaInfo &info, unsigned int)
{
    /* Since we don't throw during the ctor, check here whether we actually
     * are valid are not.  If not, die.
     */
    if (m_failed)
    {
        kdError() << "Construction of KTorrentPlugin failed for " << info.path() << endl;
        kdError() << "Aborting meta-info read.\n";
        return false;
    }
    
    TQFile file (info.path());
    if (!file.open(IO_ReadOnly))
    {
        kdError() << "Unable to open given file!\n";
        return false;
    }

    // We need to read in the entire file to parse the dictionary structure.
    TQByteArray buf = file.readAll();
    file.close();

    if (buf.isEmpty())
    {
        kdError() << "Empty file: " << info.path() << endl;
        return false;
    }

    m_dict = new BDict(buf);
    
    if (!m_dict)
    {
        kdError() << "Error creating dictionary from open file: " << info.path() << endl;
        return false;
    }

    if (!m_dict->isValid())
    {
        kdDebug(7034) << "Invalid torrent file: " << info.path() << endl;
        return false;
    }
    
    KFileMetaInfoGroup group = appendGroup(info, "TorrentInfo");

    // The remainder of this function will consist of a lot of redundancy checks.
    // If a torrent has a key, but it is of the wrong type, then it isn't a valid
    // torrent, and so we should just die.
    
    if (m_dict->contains("announce"))
    {
        BString *str = m_dict->findStr ("announce");
        if (!str)
            return false;
        appendItem (group, "announce", TQString(str->get_string()));
    }
    
    if (m_dict->contains("creation date"))
    {
        BInt *the_data = m_dict->findInt ("creation date");
        TQDateTime my_date;
        
        if (!the_data)
            return false;

        unsigned int the_time = the_data->get_value();
        
        /* Hopefully the_time is UTC, because that's what TQDateTime does. */
        my_date.setTime_t (the_time);
        appendItem (group, "creation date", my_date);
    }

    // A valid torrent must have the info dict, no reason to check twice for
    // it.
    BDict *info_dict = m_dict->findDict("info");
    int num_files = 1;
    TQ_ULLONG length = 0;
    
    if (!info_dict)
        return false;
        
    if (!info_dict->contains("length"))
    {
        /* Has more than one file.  The list of files is contained in a
         * list called, appropriately enough, 'files'
         */
        BList *info_list = info_dict->findList("files");
        if (!info_list)
            return false;
        
        num_files = info_list->count();
        length = filesLength (info_list);
    }
    else
    {
        /* Only one file, let's put its length */
        BInt *blength = info_dict->findInt("length");
        if (!blength)
            return false;
            
        length = blength->get_value();
    }

    appendItem (group, "NumFiles", num_files);
    appendItem (group, "length", length);

    if (info_dict->contains("name"))
    {
        BString *str = info_dict->findStr("name");
        if (!str)
            return false;
            
        TQString real_str (str->get_string());

        if (num_files > 1 && !real_str.endsWith("/"))
            real_str.append('/');

        appendItem (group, "name", real_str);
    }

    // piece length is required as well
    BInt *piece_length = info_dict->findInt("piece length");
    if (!piece_length)
        return false;
        
    appendItem (group, "piece length", piece_length->get_value());

    if (m_dict->contains("comment"))
    {
        BString *comment = m_dict->findStr("comment");
        if (!comment)
            return false;
            
        appendItem (group, "comment", comment->get_string());
    }
    else
        appendItem (group, "comment", TQString());

    return true;
}

/* Returns a TQStringList containing file names within the list.  The list
 * should be the one contained within the info dictionary of the torrent,
 * keyed by 'files'
 */
TQStringList filesList (BList *list)
{
    TQStringList str_list, failList;

    for (unsigned int i = 0; i < list->count(); ++i)
    {
        /* Each item in this list is a dictionary, composed as follows:
         * length -> BInt (size of file)
         * path -> BList (list of strings)
         * The list of strings is used to construct directory paths.  The
         * last element of the list is the file name.
         */

        BDict *list_dict = list->indexDict(i);
        if (!list_dict)
            return failList;

        BList *list_path = list_dict->findList("path");
        if (!list_path)
            return failList;

        TQString str;
        BString *temp_str;

        if (list_path->count() > 0)
        {
            temp_str = list_path->indexStr (0);
            if (!temp_str)
                return failList;

            str.append (temp_str->get_string());
        }

        /* Construct TQString consisting of path and file name */
        for (unsigned int j = 1; j < list_path->count(); ++j)
        {
            str.append (TQDir::separator());
            temp_str = list_path->indexStr (j);
            if (!temp_str)
                return failList;

            str.append (temp_str->get_string());
        }

        str_list += str;
    }

    return str_list;
}

/* This function determines the total length of a torrent stream.
 * The list provided should be the same one provided for filesList.
 */
TQ_ULLONG filesLength (BList *list)
{
    TQ_ULLONG length = 0;

    for (unsigned int i = 0; i < list->count(); ++i)
    {
        /* Each item in this list is a dictionary, composed as follows:
         * length -> BInt (size of file)
         * path -> BList (list of strings)
         */

        BDict *list_dict = list->indexDict(i);
        if (!list_dict)
            return 0;

        BInt *bfile_len = list_dict->findInt("length");
        if (!bfile_len)
            return 0;

        length += bfile_len->get_value();
    }

    return length;
}

bool KTorrentPlugin::writeInfo(const KFileMetaInfo &info) const
{
    if (m_failed || !m_dict)
        return false;
    
    // The m_dict is ready, all we have to do is open a file, and
    // let 'er go.
    TQStringList list = info.groups();
    TQStringList::Iterator it = list.begin();
    
    for (; it != list.end(); ++it)
    {
        TQStringList list2 = info[*it].keys();
        TQStringList::Iterator it2 = list2.begin();
        
        for (; it2 != list2.end(); ++it2)
        {
            TQString key = *it2;
            
            if (info[*it][key].isModified())
            {
                // Re-enter the entry in the dictionary.
                if (key == "comment")
                {
                    BString *b_str = m_dict->findStr("comment");
                    if (!b_str)
                        return false;

                    b_str->setValue (info[*it][key].value().toString());
                }
                else if (key == "name")
                {
                    BDict *info_dict = m_dict->findDict ("info");
                    if (!info_dict)
                        return false;

                    BString *name_str = info_dict->findStr ("name");
                    if (!name_str)
                        return false;

                    TQString the_name = info[*it][key].value().toString();
                    
                    // Remove trailing slashes
                    the_name.replace (TQRegExp("/*$"), "");
                    
                    name_str->setValue (the_name);
                }
            }
        }
    }
        
    TQFile output (info.path());
    
    if (!output.open(IO_WriteOnly | IO_Truncate))
        return false;
    
    return m_dict->writeToDevice(output);
}

#include "tdefile_torrent.moc"