summaryrefslogtreecommitdiffstats
path: root/tdecore/kicontheme.cpp
blob: 9d5d2b7d040fecdc1fa3a08f992a1524bfbf5b28 (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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/* vi: ts=8 sts=4 sw=4
 *
 * $Id$
 *
 * This file is part of the KDE project, module tdecore.
 * Copyright (C) 2000 Geert Jansen <jansen@kde.org>
 *                    Antonio Larrosa <larrosa@kde.org>
 *
 * This is free software; it comes under the GNU Library General
 * Public License, version 2. See the file "COPYING.LIB" for the
 * exact licensing terms.
 *
 * kicontheme.cpp: Lowlevel icon theme handling.
 */

#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <config.h>

#include <tqstring.h>
#include <tqstringlist.h>
#include <tqvaluelist.h>
#include <tqmap.h>
#include <tqpixmap.h>
#include <tqpixmapcache.h>
#include <tqimage.h>
#include <tqfileinfo.h>
#include <tqdir.h>

#include <kdebug.h>
#include <kstandarddirs.h>
#include <kglobal.h>
#include <tdeconfig.h>
#include <ksimpleconfig.h>
#include <kinstance.h>

#include "kicontheme.h"

class TDEIconThemePrivate
{
public:
    TQString example, screenshot;
    TQString linkOverlay, lockOverlay, zipOverlay, shareOverlay;
    bool hidden;
    TDESharedConfig::Ptr sharedConfig;
};

/**
 * A subdirectory in an icon theme.
 */
class TDEIconThemeDir
{
public:
    TDEIconThemeDir(const TQString& dir, const TDEConfigBase *config);

    bool isValid() const { return mbValid; }
    TQString iconPath(const TQString& name) const;
    TQStringList iconList() const;
    TQString dir() const { return mDir; }

    TDEIcon::Context context() const { return mContext; }
    TDEIcon::Type type() const { return mType; }
    int size() const { return mSize; }
    int minSize() const { return mMinSize; }
    int maxSize() const { return mMaxSize; }
    int threshold() const { return mThreshold; }

private:
    bool mbValid;
    TDEIcon::Type mType;
    TDEIcon::Context mContext;
    int mSize, mMinSize, mMaxSize;
    int mThreshold;

    TQString mDir;
};


/*** TDEIconTheme ***/

TDEIconTheme::TDEIconTheme(const TQString& name, const TQString& appName)
{
    d = new TDEIconThemePrivate;

    TQStringList icnlibs;
    TQStringList::ConstIterator it, itDir;
    TQStringList themeDirs;
    TQString cDir;

    // Applications can have local additions to the global "locolor" and
    // "hicolor" icon themes. For these, the _global_ theme description
    // files are used..

    if (!appName.isEmpty() &&
       ( name == "crystalsvg" || name== "hicolor" || name == "locolor" ) )
    {
	icnlibs = TDEGlobal::dirs()->resourceDirs("data");
	for (it=icnlibs.begin(); it!=icnlibs.end(); ++it)
	{
	    cDir = *it + appName + "/icons/" + name;
	    if (TQFile::exists( cDir ))
		themeDirs += cDir + "/";
	}
    }
    // Find the theme description file. These are always global.

    icnlibs = TDEGlobal::dirs()->resourceDirs("icon");
    icnlibs += TDEGlobal::dirs()->resourceDirs("xdgdata-icon");
    icnlibs += "/usr/share/pixmaps";
    // These are not in the icon spec, but e.g. GNOME puts some icons there anyway.
    icnlibs += TDEGlobal::dirs()->resourceDirs("xdgdata-pixmap");
    for (it=icnlibs.begin(); it!=icnlibs.end(); ++it)
    {
        cDir = *it + name + "/";
        if (TDEStandardDirs::exists(cDir))
        {
            themeDirs += cDir;
	    if (mDir.isEmpty()
		    && (TDEStandardDirs::exists( cDir + "index.desktop") || TDEStandardDirs::exists( cDir + "index.theme")))
		mDir = cDir;
        }
    }

    if (mDir.isEmpty())
    {
        kdDebug(264) << "Icon theme " << name << " not found.\n";
        return;
    }

    TQString fileName, mainSection;
    if(TQFile::exists(mDir + "index.desktop")) {
	fileName = mDir + "index.desktop";
	mainSection="KDE Icon Theme";
    } else {
	fileName = mDir + "index.theme";
	mainSection="Icon Theme";
    }
    // Use TDESharedConfig to avoid parsing the file many times, from each kinstance.
    // Need to keep a ref to it to make this useful
    d->sharedConfig = TDESharedConfig::openConfig( fileName, true /*readonly*/, false /*useKDEGlobals*/ );
    TDEConfig& cfg = *d->sharedConfig;
    //was: KSimpleConfig cfg(fileName);

    cfg.setGroup(mainSection);
    mName = cfg.readEntry("Name");
    mDesc = cfg.readEntry("Comment");
    mDepth = cfg.readNumEntry("DisplayDepth", 32);
    mInherits = cfg.readListEntry("Inherits");
    if ( name != "crystalsvg" )
      for ( TQStringList::Iterator it = mInherits.begin(); it != mInherits.end(); ++it )
         if ( *it == "default" || *it == "hicolor" ) *it="crystalsvg";

    d->hidden = cfg.readBoolEntry("Hidden", false);
    d->example = cfg.readPathEntry("Example");
    d->screenshot = cfg.readPathEntry("ScreenShot");
    d->linkOverlay = cfg.readEntry("LinkOverlay", "link");
    d->lockOverlay = cfg.readEntry("LockOverlay", "lock");
    d->zipOverlay = cfg.readEntry("ZipOverlay", "zip");
    d->shareOverlay = cfg.readEntry("ShareOverlay","share");

    TQStringList dirs = cfg.readPathListEntry("Directories");
    mDirs.setAutoDelete(true);
    for (it=dirs.begin(); it!=dirs.end(); ++it)
    {
	cfg.setGroup(*it);
	for (itDir=themeDirs.begin(); itDir!=themeDirs.end(); ++itDir)
	{
	    if (TDEStandardDirs::exists(*itDir + *it + "/"))
	    {
	        TDEIconThemeDir *dir = new TDEIconThemeDir(*itDir + *it, &cfg);
	        if (!dir->isValid())
	        {
	            kdDebug(264) << "Icon directory " << *itDir << " group " << *it << " not valid.\n";
	            delete dir;
	        }
	        else
	            mDirs.append(dir);
            }
        }
    }

    // Expand available sizes for scalable icons to their full range
    int i;
    TQMap<int,TQValueList<int> > scIcons;
    for (TDEIconThemeDir *dir=mDirs.first(); dir!=0L; dir=mDirs.next())
    {
        if ((dir->type() == TDEIcon::Scalable) && !scIcons.contains(dir->size()))
        {
            TQValueList<int> lst;
            for (i=dir->minSize(); i<=dir->maxSize(); i++)
                lst += i;
            scIcons[dir->size()] = lst;
        }
    }

    TQStringList groups;
    groups += "Desktop";
    groups += "Toolbar";
    groups += "MainToolbar";
    groups += "Small";
    groups += "Panel";
    const int defDefSizes[] = { 32, 22, 22, 16, 32 };
    cfg.setGroup(mainSection);
    for (it=groups.begin(), i=0; it!=groups.end(); ++it, i++)
    {
        mDefSize[i] = cfg.readNumEntry(*it + "Default", defDefSizes[i]);
        TQValueList<int> exp, lst = cfg.readIntListEntry(*it + "Sizes");
        TQValueList<int>::ConstIterator it2;
        for (it2=lst.begin(); it2!=lst.end(); ++it2)
        {
            if (scIcons.contains(*it2))
                exp += scIcons[*it2];
            else
                exp += *it2;
        }
        mSizes[i] = exp;
    }

}

TDEIconTheme::~TDEIconTheme()
{
    delete d;
}

bool TDEIconTheme::isValid() const
{
    return !mDirs.isEmpty();
}

bool TDEIconTheme::isHidden() const
{
    return d->hidden;
}

TQString TDEIconTheme::example() const { return d->example; }
TQString TDEIconTheme::screenshot() const { return d->screenshot; }
TQString TDEIconTheme::linkOverlay() const { return d->linkOverlay; }
TQString TDEIconTheme::lockOverlay() const { return d->lockOverlay; }
TQString TDEIconTheme::zipOverlay() const { return d->zipOverlay; }
TQString TDEIconTheme::shareOverlay() const { return d->shareOverlay; }

int TDEIconTheme::defaultSize(TDEIcon::Group group) const
{
    if ((group < 0) || (group >= TDEIcon::LastGroup))
    {
        kdDebug(264) << "Illegal icon group: " << group << "\n";
        return -1;
    }
    return mDefSize[group];
}

TQValueList<int> TDEIconTheme::querySizes(TDEIcon::Group group) const
{
    TQValueList<int> empty;
    if ((group < 0) || (group >= TDEIcon::LastGroup))
    {
        kdDebug(264) << "Illegal icon group: " << group << "\n";
        return empty;
    }
    return mSizes[group];
}

TQStringList TDEIconTheme::queryIcons(int size, TDEIcon::Context context) const
{
    int delta = 1000, dw;

    TQPtrListIterator<TDEIconThemeDir> dirs(mDirs);
    TDEIconThemeDir *dir;

    // Try to find exact match
    TQStringList result;
    for ( ; dirs.current(); ++dirs)
    {
        dir = dirs.current();
        if ((context != TDEIcon::Any) && (context != dir->context()))
            continue;
        if ((dir->type() == TDEIcon::Fixed) && (dir->size() == size))
        {
            result += dir->iconList();
            continue;
        }
        if ((dir->type() == TDEIcon::Scalable) &&
            (size >= dir->minSize()) && (size <= dir->maxSize()))
        {
            result += dir->iconList();
            continue;
        }
	if ((dir->type() == TDEIcon::Threshold) &&
            (abs(size-dir->size())<dir->threshold()))
            result+=dir->iconList();
    }

    return result;

    dirs.toFirst();

    // Find close match
    TDEIconThemeDir *best = 0L;
    for ( ; dirs.current(); ++dirs)
    {
        dir = dirs.current();
        if ((context != TDEIcon::Any) && (context != dir->context()))
            continue;
        dw = dir->size() - size;
        if ((dw > 6) || (abs(dw) >= abs(delta)))
            continue;
        delta = dw;
        best = dir;
    }
    if (best == 0L)
        return TQStringList();

    return best->iconList();
}

TQStringList TDEIconTheme::queryIconsByContext(int size, TDEIcon::Context context) const
{
    TQPtrListIterator<TDEIconThemeDir> dirs(mDirs);
    int dw;
    TDEIconThemeDir *dir;

    // We want all the icons for a given context, but we prefer icons
    // of size size . Note that this may (will) include duplicate icons
    //TQStringList iconlist[34]; // 33 == 48-16+1
    TQStringList iconlist[128]; // 33 == 48-16+1
    // Usually, only the 0, 6 (22-16), 10 (32-22), 16 (48-32 or 32-16),
    // 26 (48-22) and 32 (48-16) will be used, but who knows if someone
    // will make icon themes with different icon sizes.

    for ( ; dirs.current(); ++dirs)
    {
        dir = dirs.current();
        if ((context != TDEIcon::Any) && (context != dir->context()))
            continue;
        dw = abs(dir->size() - size);
        iconlist[(dw<127)?dw:127]+=dir->iconList();
    }

    TQStringList iconlistResult;
    for (int i=0; i<128; i++) iconlistResult+=iconlist[i];

    return iconlistResult;
}

bool TDEIconTheme::hasContext(TDEIcon::Context context) const
{
    TQPtrListIterator<TDEIconThemeDir> dirs(mDirs);
    TDEIconThemeDir *dir;

    for ( ; dirs.current(); ++dirs)
    {
        dir = dirs.current();
        if ((context == TDEIcon::Any) || (context == dir->context()))
            return true;
    }
    return false;
}

TDEIcon TDEIconTheme::iconPath(const TQString& name, int size, TDEIcon::MatchType match) const
{
    TDEIcon icon;
    TQString path;
    int delta = -1000, dw;
    TDEIconThemeDir *dir;

    dw = 1000; // shut up, gcc
    TQPtrListIterator<TDEIconThemeDir> dirs(mDirs);
    for ( ; dirs.current(); ++dirs)
    {
        dir = dirs.current();

        if (match == TDEIcon::MatchExact)
        {
            if ((dir->type() == TDEIcon::Fixed) && (dir->size() != size))
                continue;
            if ((dir->type() == TDEIcon::Scalable) &&
                ((size < dir->minSize()) || (size > dir->maxSize())))
              continue;
            if ((dir->type() == TDEIcon::Threshold) &&
		(abs(dir->size()-size) > dir->threshold()))
                continue;
        } else
        {
          // dw < 0 means need to scale up to get an icon of the requested size
          if (dir->type() == TDEIcon::Fixed)
          {
            dw = dir->size() - size;
          } else if (dir->type() == TDEIcon::Scalable)
          {
            if (size < dir->minSize())
              dw = dir->minSize() - size;
            else if (size > dir->maxSize())
              dw = dir->maxSize() - size;
            else
              dw = 0;
          } else if (dir->type() == TDEIcon::Threshold)
          {
            if (size < dir->size() - dir->threshold())
              dw = dir->size() - dir->threshold() - size;
            else if (size > dir->size() + dir->threshold())
              dw = dir->size() + dir->threshold() - size;
            else
              dw = 0;
          }
          /* Skip this if we've found a closer one, unless
             it's a downscale, and we only had upscales befores.
             This is to avoid scaling up unless we have to,
             since that looks very ugly */
          if (/*(abs(dw) >= abs(delta)) ||*/
              (delta > 0 && dw < 0))
            continue;
        }

        path = dir->iconPath(name);
        if (path.isEmpty())
            continue;
        icon.path = path;
        icon.size = dir->size();
        icon.type = dir->type();
	icon.threshold = dir->threshold();
        icon.context = dir->context();

        // if we got in MatchExact that far, we find no better
        if (match == TDEIcon::MatchExact)
            return icon;
	else
        {
	    delta = dw;
	    if (delta==0) return icon; // We won't find a better match anyway
        }
    }
    return icon;
}

// static
TQString *TDEIconTheme::_theme = 0L;

// static
TQStringList *TDEIconTheme::_theme_list = 0L;

// static
TQString TDEIconTheme::current()
{
    // Static pointer because of unloading problems wrt DSO's.
    if (_theme != 0L)
        return *_theme;

    _theme = new TQString();
    TDEConfig *config = TDEGlobal::config();
    TDEConfigGroupSaver saver(config, "Icons");
    *_theme = config->readEntry("Theme",defaultThemeName());
    if ( *_theme == TQString::fromLatin1("hicolor") ) *_theme = defaultThemeName();
/*    if (_theme->isEmpty())
    {
        if (TQPixmap::defaultDepth() > 8)
            *_theme = defaultThemeName();
        else
            *_theme = TQString::fromLatin1("locolor");
    }*/
    return *_theme;
}

// static
TQStringList TDEIconTheme::list()
{
    // Static pointer because of unloading problems wrt DSO's.
    if (_theme_list != 0L)
        return *_theme_list;

    _theme_list = new TQStringList();
    TQStringList icnlibs = TDEGlobal::dirs()->resourceDirs("icon");
    icnlibs += (TDEGlobal::dirs()->resourceDirs("xdgdata-icon"));
    icnlibs += "/usr/share/pixmaps";
    // These are not in the icon spec, but e.g. GNOME puts some icons there anyway.
    icnlibs += TDEGlobal::dirs()->resourceDirs("xdgdata-pixmap");
    TQStringList::ConstIterator it;
    for (it=icnlibs.begin(); it!=icnlibs.end(); ++it)
    {
        TQDir dir(*it);
        if (!dir.exists())
            continue;
        TQStringList lst = dir.entryList(TQDir::Dirs);
        TQStringList::ConstIterator it2;
        for (it2=lst.begin(); it2!=lst.end(); ++it2)
        {
            if ((*it2 == ".") || (*it2 == "..") || (*it2).startsWith("default.") )
                continue;
            if (!TDEStandardDirs::exists(*it + *it2 + "/index.desktop") && !TDEStandardDirs::exists(*it + *it2 + "/index.theme"))
                continue;
		TDEIconTheme oink(*it2);
	    if (!oink.isValid()) continue;

	    if (!_theme_list->contains(*it2))
                _theme_list->append(*it2);
        }
    }
    return *_theme_list;
}

// static
void TDEIconTheme::reconfigure()
{
    delete _theme;
    _theme=0L;
    delete _theme_list;
    _theme_list=0L;
}

// static
TQString TDEIconTheme::defaultThemeName()
{
    return TQString::fromLatin1("crystalsvg");
}

/*** TDEIconThemeDir ***/

TDEIconThemeDir::TDEIconThemeDir(const TQString& dir, const TDEConfigBase *config)
{
    mbValid = false;
    mDir = dir;
    mSize = config->readNumEntry("Size");
    mMinSize = 1;    // just set the variables to something
    mMaxSize = 50;   // meaningful in case someone calls minSize or maxSize
    mType = TDEIcon::Fixed;

    if (mSize == 0)
        return;

    TQString tmp = config->readEntry("Context");
    if (tmp == "Devices")
        mContext = TDEIcon::Device;
    else if (tmp == "MimeTypes")
        mContext = TDEIcon::MimeType;
    else if (tmp == "FileSystems")
        mContext = TDEIcon::FileSystem;
    else if (tmp == "Applications")
        mContext = TDEIcon::Application;
    else if (tmp == "Actions")
        mContext = TDEIcon::Action;
    else if (tmp == "Animations")
        mContext = TDEIcon::Animation;
    else if (tmp == "Categories")
        mContext = TDEIcon::Category;
    else if (tmp == "Emblems")
        mContext = TDEIcon::Emblem;
    else if (tmp == "Emotes")
        mContext = TDEIcon::Emote;
    else if (tmp == "International")
        mContext = TDEIcon::International;
    else if (tmp == "Places")
        mContext = TDEIcon::Place;
    else if (tmp == "Status")
        mContext = TDEIcon::StatusIcon;
    else {
        kdDebug(264) << "Invalid Context= line for icon theme: " << mDir << "\n";
        return;
    }
    tmp = config->readEntry("Type");
    if (tmp == "Fixed")
        mType = TDEIcon::Fixed;
    else if (tmp == "Scalable")
        mType = TDEIcon::Scalable;
    else if (tmp == "Threshold")
        mType = TDEIcon::Threshold;
    else {
        kdDebug(264) << "Invalid Type= line for icon theme: " <<  mDir << "\n";
        return;
    }
    if (mType == TDEIcon::Scalable)
    {
        mMinSize = config->readNumEntry("MinSize", mSize);
        mMaxSize = config->readNumEntry("MaxSize", mSize);
    } else if (mType == TDEIcon::Threshold)
	mThreshold = config->readNumEntry("Threshold", 2);
    mbValid = true;
}

TQString TDEIconThemeDir::iconPath(const TQString& name) const
{
    if (!mbValid)
        return TQString::null;
    TQString file = mDir + "/" + name;

    if (access(TQFile::encodeName(file), R_OK) == 0)
        return file;

    return TQString::null;
}

TQStringList TDEIconThemeDir::iconList() const
{
    TQDir dir(mDir);
#ifdef HAVE_LIBART
    TQStringList lst = dir.entryList("*.png;*.svg;*.svgz;*.xpm", TQDir::Files);
#else
    TQStringList lst = dir.entryList("*.png;*.xpm", TQDir::Files);
#endif
    TQStringList result;
    TQStringList::ConstIterator it;
    for (it=lst.begin(); it!=lst.end(); ++it)
        result += mDir + "/" + *it;
    return result;
}