summaryrefslogtreecommitdiffstats
path: root/ksquirrel/sq_libraryhandler.cpp
blob: 51d5e9bf004b4651275b10c08686f15fc836683c (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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/***************************************************************************
                          sq_libraryhandler.cpp  -  description
                             -------------------
    begin                : Mar 5 2004
    copyright            : (C) 2004 by Baryshev Dmitry
    email                : ksquirrel.iv@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program 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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <tqlibrary.h>
#include <tqfileinfo.h>
#include <tqstringlist.h>
#include <tqfile.h>
#include <tqdir.h>

#include <kstringhandler.h>
#include <tdetempfile.h>
#include <tdeconfig.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <kurl.h>
#include <kmimetype.h>

#include "sq_libraryhandler.h"
#include "sq_config.h"

#include <ksquirrel-libs/fmt_codec_base.h>

#include <iostream>
#include <iomanip>

static const int buffer_size = 10;

SQ_LibraryHandler * SQ_LibraryHandler::m_instance = 0;

// SQ_LibraryHandler
SQ_LibraryHandler::SQ_LibraryHandler(TQObject *parent) 
    : TQObject(parent), TQValueVector<SQ_LIBRARY>()
{
    m_instance = this;

    kdDebug() << "+SQ_LibraryHandler" << endl;

    tdeconf = new TDEConfig("ksquirrel-codec-settings");

    load();
}

SQ_LibraryHandler::~SQ_LibraryHandler()
{
    clear();

    delete tdeconf;

    kdDebug() << "-SQ_LibraryHandler" << endl;
}

/*
 *  Find appropriate SQ_LIBRARY by filename. If
 *  not found, return NULL.
 */
SQ_LIBRARY* SQ_LibraryHandler::libraryForFile(const KURL &url)
{
    KMimeType::Ptr mime = KMimeType::findByURL(url);

    iterator itEnd = end();

    SQ_LIBRARY *l = 0;

    // go through array and compare names
    for(iterator it = begin();it != itEnd;++it)
    {
        if((*it).mime_multi)
        {
            if((*it).mimetype.find(mime->name()) != -1)
            {
                l = &(*it);
                break;
            }
        }
        else if((*it).mimetype == mime->name())
        {
            l = &(*it);
            break;
        }
    }

#if 0
    if(l)
        kdDebug() << KStringHandler::lsqueeze(url.prettyURL())
                  << "\" => "
                  << l->quickinfo
                  << endl;
#endif

    return l;
}

SQ_LIBRARY* SQ_LibraryHandler::libraryForFile(const TQString &path)
{
    KURL u;
    u.setPath(path);

    return libraryForFile(u);
}

/*
 *  Get all filters as one string.
 */
TQString SQ_LibraryHandler::allFiltersString() const
{
    TQString ret;

    const_iterator itEnd = end();

    // construct string
    for(const_iterator it = begin();it != itEnd;++it)
    {
        if(!(*it).filter.isEmpty())
            ret = ret + (*it).filter + ' ';
    }

    return ret;
}

TQString SQ_LibraryHandler::allFiltersFileDialogString(bool r, bool allfiles) const
{
    TQString ret;

    const_iterator itEnd = end();

    // construct string
    for(const_iterator it = begin();it != itEnd;++it)
    {
        if(!r)
            if((*it).writestatic)
                ret = ret + (*it).filter + '|' + (*it).quickinfo + '\n';
            else;
        else if((*it).readable)
            ret = ret + (*it).filter + '|' + (*it).quickinfo + '\n';
    }

    return allfiles ? (ret + "*.*|" + i18n("All files")) : ret.left(ret.length() - 1);
}

/*
 *  Fill 'filters' with all found filters, and
 *  'quick' with appropriate information.
 */
void SQ_LibraryHandler::allFilters(TQStringList &filters, TQStringList &quick) const
{
    // clear rubbish
    filters.clear();
    quick.clear();

    // no found libraries ?
    if(empty())
        return;

    const_iterator itEnd = end();

    // go through array and fill TQStringLists
    for(const_iterator it = begin();it != itEnd;++it)
        if(!(*it).filter.isEmpty())
        {
            filters.append((*it).filter);
            quick.append((*it).quickinfo);
        }
}

void SQ_LibraryHandler::allWritableFilters(TQStringList &filters, TQStringList &quick) const
{
    // clear rubbish
    filters.clear();
    quick.clear();

    // no libraries ?
    if(empty())
        return;

    const_iterator itEnd = end();

    // go through array and fill TQStringLists
    for(const_iterator it = begin();it != itEnd;++it)
        if((*it).writestatic && !(*it).filter.isEmpty())
        {
            filters.append((*it).filter);
            quick.append((*it).quickinfo);
        }
}

/*
 *  Remove and unload all libraries.
 */
void SQ_LibraryHandler::clear()
{
    kdDebug() << "SQ_LibraryHandler::clear()" << endl;

    iterator itEnd = end();

    // unload libraries on clear()
    for(iterator it = begin();it != itEnd;++it)
    {
        writeSettings(&(*it));

        // delete temp file
        if((*it).needtempfile)
        {
            delete (*it).tmp_il;
            delete (*it).tmp;
        }

        (*it).codec_destroy((*it).codec_il);
        (*it).codec_destroy((*it).codec);
        delete (*it).lib;
        (*it).lib = 0;
    }

    TQValueVector<SQ_LIBRARY>::clear();
}

/*
 *  Add new libraries.
 */
void SQ_LibraryHandler::add(TQStringList &foundLibraries)
{
    codec_options o;

    TQStringList::iterator itEnd = foundLibraries.end();

    for(TQStringList::iterator it = foundLibraries.begin();it != itEnd;++it)
    {
        TQFileInfo ff(*it);

        SQ_LIBRARY libtmp;

        // create TQLibrary object
        libtmp.lib = new TQLibrary(*it);
        libtmp.libpath = *it;
        libtmp.lib->load();

        // resolve create() and destroy() functions
        libtmp.codec_create = (fmt_codec_base*(*)())(libtmp.lib)->resolve(TQString::fromLatin1("codec_create"));
        libtmp.codec_destroy = (void (*)(fmt_codec_base*))(libtmp.lib)->resolve(TQString::fromLatin1("codec_destroy"));

        // couldn't resolve - corrupted library ?
        if(!libtmp.codec_create || !libtmp.codec_destroy)
        {
            libtmp.lib->unload();
            delete libtmp.lib;
        }
        else
        {
            // create codec !
            fmt_codec_base *codeK = libtmp.codec_create();

            // read options
            codeK->options(&o);

            TQString q = o.name;

            // Yet unknown library ?
            if(!alreadyInMap(q))
            {
                libtmp.mime = TQPixmap(reinterpret_cast<const char **>(o.pixmap));
                libtmp.mimetype = o.mimetype;
                libtmp.mime_multi = libtmp.mimetype.find(';') != -1;
                libtmp.quickinfo = q;
                libtmp.filter = o.filter;
                libtmp.version = o.version;
                libtmp.regexp_str = o.mime;
                libtmp.config = o.config;
                libtmp.regexp.setPattern(libtmp.regexp_str);
                libtmp.regexp.setCaseSensitive(true);
                libtmp.writestatic = o.writestatic;
                libtmp.writeanimated = o.writeanimated;
                libtmp.readable = o.readable;
                libtmp.canbemultiple = o.canbemultiple;
                libtmp.needtempfile = o.needtempfile;
                libtmp.tmp = 0;

                libtmp.codec_il = libtmp.codec_create();

                if(libtmp.needtempfile)
                {
                    libtmp.tmp = new KTempFile;
                    libtmp.tmp->setAutoDelete(true);
                    libtmp.tmp->close();
                    codeK->settempfile(libtmp.tmp->name().ascii());

                    libtmp.tmp_il = new KTempFile;
                    libtmp.tmp_il->setAutoDelete(true);
                    libtmp.tmp_il->close();
                    libtmp.codec_il->settempfile(libtmp.tmp_il->name().ascii());
                }

                if(libtmp.writestatic)
                    codeK->getwriteoptions(&libtmp.opt);

                libtmp.codec = codeK;

                readSettings(&libtmp);

                append(libtmp);
            }
            else // already known library
            {
                // destroy codec
                libtmp.codec_destroy(codeK);

                // unload library
                libtmp.lib->unload();

                delete libtmp.lib;
            }
        }
    }

    // print some information
    dump();
}

/*
 *  Is library named 'quick' already been handled ?
 */
bool SQ_LibraryHandler::alreadyInMap(const TQString &quick) const
{
    const_iterator itEnd = end();

    // go through array and find 'quick'
    for(const_iterator it = begin();it != itEnd;++it)
        if((*it).quickinfo == quick)
            return true;

    return false;
}

/*
 *  Print some information on found libraries.
 */
void SQ_LibraryHandler::dump() const
{
    std::cerr << "SQ_LibraryHandler: memory dump (total " << count() << ")" << endl;

    const_iterator itEnd = end();

    std::cerr.setf(ios::left);

    for(const_iterator it = begin();it != itEnd;++it)
    {
        std::cerr << std::setw(30)
                  << KStringHandler::csqueeze(TQFileInfo((*it).libpath).fileName(), 30)
                  << std::setw(0)
                  << "  [" 
                  << KStringHandler::rsqueeze((*it).quickinfo, 45)
                  << "]"
                  << endl;
    }
}

/*
 *  Does any of found libraries handle given extension ?
 */
bool SQ_LibraryHandler::knownExtension(const TQString &ext)
{
    iterator itEnd = end();

    // go through array and compare extensions
    for(iterator it = begin();it != itEnd;++it)
    {
        if((*it).filter.contains(ext, false))
            return true;
    }

    return false;
}

/*
 *  Find appropriate SQ_LIBRARY by its name. If
 *  not found, return NULL.
 *
 *  Name is a string, returned by fmt_quickinfo()
 */
SQ_LIBRARY* SQ_LibraryHandler::libraryByName(const TQString &name)
{
    SQ_LIBRARY *l;

    iterator itEnd = end();

    // go through array and compare names
    for(iterator it = begin();it != itEnd;++it)
    {
        l = &(*it);

        if(l->quickinfo == name)
            return l;
    }

    return 0;
}

void SQ_LibraryHandler::writeSettings(SQ_LIBRARY *lib)
{
    // no config - no settings
    if(lib->config.isEmpty())
        return;

    tdeconf->setGroup(lib->quickinfo);

    fmt_settings::iterator itEnd = lib->settings.end();

    TQString k;

    for(fmt_settings::iterator it = lib->settings.begin();it != itEnd;++it)
    {
        k = (*it).first;

        if((*it).second.type == settings_value::v_bool) // boolean
        {
           k.prepend("b");
           tdeconf->writeEntry(k, (*it).second.bVal);
        }
        else if((*it).second.type == settings_value::v_int) // integer
        {
            k.prepend("i");
            tdeconf->writeEntry(k, (*it).second.iVal);
        }
        else if((*it).second.type == settings_value::v_double) // double
         {
             k.prepend("d");
             tdeconf->writeEntry(k, (*it).second.dVal);
         }
        else // string
        {
            k.prepend("s");
            tdeconf->writeEntry(k, (*it).second.sVal);
        }
    }
}

void SQ_LibraryHandler::readSettings(SQ_LIBRARY *lib)
{
    // no config - no settings
    if(lib->config.isEmpty())
        return;

    TQMap<TQString, TQString> map = tdeconf->entryMap(lib->quickinfo);

    if(!map.size())
    {
        lib->codec->fill_default_settings();
        lib->settings = lib->codec->settings();
        return;
    }

    TQMap<TQString, TQString>::iterator mapEnd = map.end();
    fmt_settings &sett = lib->settings;
    TQString d, k;
    settings_value val;

    for(TQMap<TQString, TQString>::iterator mapIt = map.begin();mapIt != mapEnd;++mapIt)
    {
        k = mapIt.key();
        d = mapIt.data();

        if(k.startsWith(TQChar('i')))
        {
            val.type = settings_value::v_int;
            val.iVal = d.toInt();
        }
        else if(k.startsWith(TQChar('d')))
        {
            val.type = settings_value::v_double;
            val.dVal = d.toDouble();
        }
        else if(k.startsWith(TQChar('b')))
        {
            val.type = settings_value::v_bool;
            val.bVal = (d == "true");
        }
        else // all other values are treated as strings
        {
            val.type = settings_value::v_string;
            val.sVal = d.ascii();
        }

        k = k.right(k.length() - 1);
        sett[k.ascii()] = val;
    }

    lib->codec->set_settings(sett);
}

void SQ_LibraryHandler::reload()
{
    clear();
    load();
}

void SQ_LibraryHandler::load()
{
    TQStringList libs;

    TQDir dir(SQ_KLIBS, TQString(), TQDir::Unsorted, TQDir::Files);

    const TQFileInfoList *list = dir.entryInfoList();

    if(list)
    {
        TQFileInfoListIterator it(*list);
        TQFileInfo *fi;

        while((fi = it.current()) != 0)
        {
            libs.append(fi->absFilePath());
            ++it;
        }
    }

    // just show dump, if no libs were found
    add(libs);
}

SQ_LibraryHandler::Support SQ_LibraryHandler::maybeSupported(const KURL &u, const TQString &mime) const
{
    const_iterator itEnd = constEnd();

    SQ_Config::instance()->setGroup("Main");
    bool treat =  SQ_Config::instance()->readBoolEntry("treat", true);

    // we can determine mimetype by hand or use "mime"
    TQString mimeDet = mime.isEmpty() ? KMimeType::findByURL(u)->name() : mime;

    // mimetype by magic is not determined automatically
    // for non-local urls - we may support this file type or may not
    // (we don't know exactly at this moment)
    if(!u.isLocalFile() && mimeDet == KMimeType::defaultMimeType())
        return (treat ? SQ_LibraryHandler::No : SQ_LibraryHandler::Maybe);

    // go through array and compare mimetype names
    for(const_iterator it = constBegin();it != itEnd;++it)
    {
        if((*it).mime_multi)
        {
            if((*it).mimetype.find(mimeDet, 0, false) != -1)
                return SQ_LibraryHandler::Yes;
        }
        else if((*it).mimetype == mimeDet) // don't waste CPU time with find()
            return SQ_LibraryHandler::Yes;
    }

    // we don't know about given mimetype
    return SQ_LibraryHandler::No;
}

void SQ_LibraryHandler::sync()
{
    iterator itEnd = end();

    // unload libraries on clear()
    for(iterator it = begin();it != itEnd;++it)
        writeSettings(&(*it));

    tdeconf->sync();
}