~ubuntu-branches/ubuntu/trusty/kvirc/trusty

« back to all changes in this revision

Viewing changes to src/kvirc/kernel/kvi_iconmanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Kai Wasserbäch, Kai Wasserbäch, Raúl Sánchez Siles
  • Date: 2011-02-12 10:40:21 UTC
  • mfrom: (14.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110212104021-5mh4f75jlku20mnt
The combined "Twisted Experiment" and "Nocturnal Raid" release.

[ Kai Wasserbäch ]
* Synced to upstream's SVN revision 5467.
* debian/rules:
  - Added .PHONY line.
  - Resurrect -DMANUAL_REVISION, got lost somewhere and we build SVN
    revisions again.
  - Replace "-DWITH_NO_EMBEDDED_CODE=YES" with "-DWANT_CRYPTOPP=YES".
  - Change the remaining -DWITH/-DWITHOUT to the new -DWANT syntax.
* debian/control:
  - Removed DMUA, I'm a DD now.
  - Changed my e-mail address.
  - Removed unneeded relationships (no upgrades over two releases are
    supported).
  - Fix Suggests for kvirc-dbg.
  - kvirc-data: Make the "Suggests: kvirc" a Recommends, doesn't make much
    sense to install the -data package without the program.
* debian/source/local-options: Added with "unapply-patches".
* debian/kvirc.lintian-overrides: Updated to work for 4.1.1.
* debian/patches/21_make_shared-mime-info_B-D_superfluous.patch: Updated.
* debian/kvirc-data.install: Added .notifyrc.

[ Raúl Sánchez Siles ]
* Stating the right version where kvirc-data break and replace should happen.
* Fixing link to license file.
* Added French and Portuguese man pages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//=============================================================================
2
 
//
3
 
//   File : kvi_iconmanager.cpp
4
 
//   Creation date : Sat Jun 24 2000 14:50:11 by Szymon Stefanek
5
 
//
6
 
//   This file is part of the KVirc irc client distribution
7
 
//   Copyright (C) 2000-2008 Szymon Stefanek (pragma at kvirc dot net)
8
 
//
9
 
//   This program is FREE software. You can redistribute it and/or
10
 
//   modify it under the terms of the GNU General Public License
11
 
//   as published by the Free Software Foundation; either version 2
12
 
//   of the License, or (at your opinion) any later version.
13
 
//
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.
17
 
//   See the GNU General Public License for more details.
18
 
//
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 Foundation,
21
 
//   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
 
//
23
 
//=============================================================================
24
 
 
25
 
 
26
 
#define _KVI_ICONMANAGER_CPP_
27
 
 
28
 
#include "kvi_iconmanager.h"
29
 
#include "kvi_app.h"
30
 
#include "kvi_settings.h"
31
 
#include "kvi_defaults.h"
32
 
#include "kvi_locale.h"
33
 
#include "kvi_fileutils.h"
34
 
#include "kvi_options.h"
35
 
 
36
 
#include <QLayout>
37
 
#include <QLabel>
38
 
#include <QCursor>
39
 
#include <QEvent>
40
 
#include <QCloseEvent>
41
 
#include <QIcon>
42
 
#include <QDir>
43
 
 
44
 
// kvi_app.cpp
45
 
extern QPixmap * g_pUserChanStatePixmap;
46
 
extern QPixmap * g_pActivityMeterPixmap;
47
 
 
48
 
KviIconManager * g_pIconManager = 0;
49
 
 
50
 
static const char * g_szIconNames[KVI_NUM_SMALL_ICONS]=
51
 
{
52
 
        "none", // 0
53
 
        "close", // 1
54
 
        "maximize", // 2
55
 
        "minimize", // 3
56
 
        "restore", // 4
57
 
        "default", // 5
58
 
        "dock", // 6
59
 
        "undock", // 7
60
 
        "quitapp", // 8
61
 
        "console", // 9
62
 
        "floppy", // 10
63
 
        "parsererror", // 11
64
 
        "parserwarning", // 12
65
 
        "server", // 13
66
 
        "world", // 14
67
 
        "proxy", // 15
68
 
        "kvirc", // 16
69
 
        "cut", // 17
70
 
        "copy", // 18
71
 
        "paste", // 19
72
 
        "options", // 20
73
 
        "socketmessage", // 21
74
 
        "socketwarning", // 22
75
 
        "socketerror", // 23
76
 
        "systemerror", // 24
77
 
        "raw", // 25
78
 
        "systemwarning", // 26
79
 
        "systemmessage", // 27
80
 
        "unhandled", // 28
81
 
        "serverinfo", // 29
82
 
        "motd", // 30
83
 
        "channel", // 31
84
 
        "hidedoubleview", // 32
85
 
        "showdoubleview", // 33
86
 
        "op", // 34
87
 
        "voice", // 35
88
 
        "serverping", // 36
89
 
        "showlistview", // 37
90
 
        "hidelistview", // 38
91
 
        "join", // 39
92
 
        "part", // 40
93
 
        "unrecognized", // 41
94
 
        "topic", // 42
95
 
        "accept", // 43
96
 
        "discard", // 44
97
 
        "ownprivmsg", // 45
98
 
        "chanprivmsg", // 46
99
 
        "query", // 48
100
 
        "queryprivmsg", // 49
101
 
        "help", // 49
102
 
        "mdihelp", // 50
103
 
        "ctcpreply", // 51
104
 
        "ctcprequestreplied", // 52
105
 
        "ctcprequestignored", // 53
106
 
        "ctcprequestflood", // 54
107
 
        "ctcprequestunknown", // 55
108
 
        "action", // 56
109
 
        "avatar", // 57
110
 
        "quit", // 58
111
 
        "split", // 59
112
 
        "quitsplit", // 60
113
 
        "nick", // 61
114
 
        "deop", // 62
115
 
        "devoice", // 63
116
 
        "mode", // 64
117
 
        "key", // 65
118
 
        "limit", // 66
119
 
        "ban", // 67
120
 
        "unban", // 68
121
 
        "banexcept", // 69
122
 
        "banunexcept", // 70
123
 
        "inviteexcept", // 71
124
 
        "inviteunexcept", // 72
125
 
        "chanmode", // 73
126
 
        "chanmodehide", // 74
127
 
        "who", // 75
128
 
        "editor", // 76
129
 
        "dccrequest", // 77
130
 
        "dccmsg", // 78
131
 
        "dccerror", // 79
132
 
        "iconmanager", // 80
133
 
        "scriptcenter", // 81
134
 
        "bomb", // 82
135
 
        "event", // 83
136
 
        "eventnohandlers", // 84
137
 
        "handler", // 85
138
 
        "handlerdisabled", // 86
139
 
        "nicknameproblem", // 87
140
 
        "whoisuser", // 88
141
 
        "whoischannels", // 89
142
 
        "whoisidle", // 90
143
 
        "whoisserver", // 91
144
 
        "whoisother", // 92
145
 
        "time", // 93
146
 
        "notifyonline", // 94
147
 
        "notifyoffline", // 95
148
 
        "locked", // 96
149
 
        "unlocked", // 97
150
 
        "lockedoff", // 98
151
 
        "unlockedoff", // 99
152
 
        "ownprivmsgcrypted", // 100
153
 
        "chanprivmsgcrypted", // 101
154
 
        "queryprivmsgcrypted", // 102
155
 
        "dccchatmsg", // 103
156
 
        "dccchatmsgcrypted", // 104
157
 
        "irc", // 105
158
 
        "folder", // 106
159
 
        "home", // 107
160
 
        "bookmarks", // 108
161
 
        "spy", // 109
162
 
        "kick", // 110
163
 
        "linux", // 111
164
 
        "links", // 112
165
 
        "regusers", // 113
166
 
        "dockwidget", // 114
167
 
        "cascadewindows", // 115
168
 
        "maxvertical", // 116
169
 
        "maxhorizontal", // 117
170
 
        "tilewindows", // 118
171
 
        "log", // 119
172
 
        "spam", // 120
173
 
        "file", // 121
174
 
        "icq", // 122
175
 
        "icqyellow", // 123
176
 
        "icqred", // 124
177
 
        "icqblue", // 125
178
 
        "icqlightgreen", // 126
179
 
        "icqlightyellow", // 127
180
 
        "message", // 128
181
 
        "messagesent", // 129
182
 
        "bluesquare", // 130
183
 
        "violetsquare", // 131
184
 
        "yellowsquare", // 132
185
 
        "greensquare", // 133
186
 
        "blacksquare", // 134
187
 
        "redsquare", // 135
188
 
        "cyansquare", // 136
189
 
        "dkgreensquare", // 137
190
 
        "terminal", // 138
191
 
        "wallops", // 139
192
 
        "invisible", // 140
193
 
        "servernotice", // 141
194
 
        "gnutella", // 142
195
 
        "search", // 143
196
 
        "files", // 144
197
 
        "transfer", // 145
198
 
        "package", // 146
199
 
        "retry", // 147
200
 
        "idea", // 148
201
 
        "colors", // 149
202
 
        "gui", // 150
203
 
        "ircview", // 151
204
 
        "alias", // 152
205
 
        "channelnotice",// 153
206
 
        "channelnoticecrypted", // 154
207
 
        "querynotice", // 155
208
 
        "querynoticecrypted", // 156
209
 
        "servnotice", // 157
210
 
        "popup", // 158
211
 
        "prologue", // 159
212
 
        "epilogue", // 160
213
 
        "sharedfiles", // 161
214
 
        "ctcpreplyunknown", // 162
215
 
        "canvas", // 163
216
 
        "nickserv", // 164
217
 
        "chanserv", // 165
218
 
        "dccvoice", // 166
219
 
        "play", // 167
220
 
        "record", // 168
221
 
        "autotilewindows", // 169
222
 
        "away", // 170
223
 
        "ident", // 171
224
 
        "homepage", // 172
225
 
        "list", // 173
226
 
        "halfop", // 174
227
 
        "halfdeop", // 175
228
 
        "invite", // 176
229
 
        "multimedia", // 177
230
 
        "look", // 178
231
 
        "input", // 179
232
 
        "messages", // 180
233
 
        "querytrace", // 181
234
 
        "nochannel", // 182
235
 
        "broadcastprivmsg", // 183
236
 
        "broadcastnotice", // 184
237
 
        "url", // 185
238
 
        "rawevent", // 186
239
 
        "raweventnohandlers", // 187
240
 
        "mekick", // 188
241
 
        "meop", // 189
242
 
        "mevoice", // 190
243
 
        "medeop", // 191
244
 
        "medevoice", // 192
245
 
        "mehalfop", // 193
246
 
        "medehalfop", // 194
247
 
        "meban", // 195
248
 
        "meunban", // 196
249
 
        "mebanexcept", // 197
250
 
        "mebanunexcept", // 198
251
 
        "meinviteexcept", // 199
252
 
        "meinviteunexcept", // 200
253
 
        "classicwindowlist", // 201
254
 
        "treewindowlist", // 202
255
 
        "ignore", // 203
256
 
        "userlist",// 204
257
 
        "stats", // 205
258
 
        "popupmenu", // 206
259
 
        "xy", // 207
260
 
        "irc0", // 208
261
 
        "irc1", // 209
262
 
        "irc2", // 210
263
 
        "irc3", // 211
264
 
        "irc4", // 212
265
 
        "irc5", // 213
266
 
        "heart", // 214
267
 
        "heartbroken", // 215
268
 
        "rose", // 215
269
 
        "biggrin", // 217
270
 
        "biggringlasses", // 218
271
 
        "biggrineyes", // 219
272
 
        "textexclamative", // 220
273
 
        "textpoints", // 221
274
 
        "kiss", // 222
275
 
        "surprised1", // 223
276
 
        "ugly", // 224
277
 
        "angry", // 225
278
 
        "surprised2", // 226
279
 
        "smile", // 227
280
 
        "tongue", // 228
281
 
        "ssl", // 229
282
 
        "cry", // 230
283
 
        "eye", // 231
284
 
        "deadchannel", // 232
285
 
        "deadquery", // 233
286
 
        "sound", // 234
287
 
        "toolbar", // 235
288
 
        "textencoding",// 236
289
 
        "newitem", // 237
290
 
        "newitembywizard", // 238
291
 
        "deleteitem", // 239
292
 
        "edititem", // 240
293
 
        "filetransfer", // 241
294
 
        "chanadmin", // 242
295
 
        "chanunadmin", // 243
296
 
        "mechanadmin", // 244
297
 
        "mechanunadmin", // 245
298
 
        "userop", // 246
299
 
        "deuserop", // 247
300
 
        "meuserop", // 248
301
 
        "medeuserop", // 249
302
 
        "applet", // 250
303
 
        "caption", // 251
304
 
        "transparent", // 252
305
 
        "alert", // 253
306
 
        "userwindow", // 254
307
 
        "statusbar", // 255
308
 
        "notaway",  // 256
309
 
        "plus",    // 257
310
 
        "minus",    // 258
311
 
        "binarytext", // 259
312
 
        "chanowner", // 260
313
 
        "chanunowner", // 261
314
 
        "mechanowner", // 262
315
 
        "mechanunowner", // 263
316
 
        "afraid", // 264
317
 
        "tongue2", // 265
318
 
        "sayicon", // 266
319
 
        "saycolors", // 267
320
 
        "finger", // 268
321
 
        "scriptaction", // 269
322
 
        "tearsmile", // 270
323
 
        "shy", // 271
324
 
        "servererror", // 272
325
 
        "cafe", // 273
326
 
        "addons", // 274
327
 
        "chanowneraway", // 275
328
 
        "opaway", // 276
329
 
        "voiceaway", // 277
330
 
        "chanadminaway", // 278
331
 
        "useropaway", // 279
332
 
        "halfopaway", // 280
333
 
        "classeditor", // 281
334
 
        "demoralized", // 282
335
 
        "slurp", // 283
336
 
        "namespace",  // 284
337
 
        "saysmile", // 285
338
 
        "saykvs", // 286
339
 
        "themeoptions", // 287
340
 
        "bug", // 288
341
 
        "refresh", //289
342
 
        "theme", // 290
343
 
        "screenshot", // 291
344
 
        "update", // 292
345
 
        "notupdate", // 293
346
 
        "failupdate", // 294
347
 
        "unreadtext", // 295
348
 
        "ircop", // 296
349
 
        "ircopaway", // 297
350
 
        "deircop", // 298
351
 
        "meircop", // 299
352
 
        "medeircop", // 300
353
 
        "angel", // 301
354
 
        "clown", // 302
355
 
        "devil", // 303
356
 
        "inlove", // 304
357
 
        "ninja", // 305
358
 
        "pirate", // 306
359
 
        "puke", // 307
360
 
        "rage", // 308
361
 
        "class", // 309
362
 
        "classnotbuilt", // 310
363
 
        "function" // 311
364
 
};
365
 
 
366
 
KviIconWidget::KviIconWidget()
367
 
: QWidget(0 /*,WType_TopLevel | WStyle_Customize | WStyle_Title | WStyle_StaysOnTop | WStyle_DialogBorder | WStyle_SysMenu | WStyle_Minimize*/)
368
 
{
369
 
        setObjectName("global_icon_widget");
370
 
        init();
371
 
}
372
 
 
373
 
KviIconWidget::KviIconWidget(QWidget * par)
374
 
: QWidget(par)
375
 
{
376
 
        setObjectName("global_icon_widget");
377
 
        init();
378
 
}
379
 
 
380
 
void KviIconWidget::init()
381
 
{
382
 
        setWindowTitle(__tr2qs("Icon Table"));
383
 
        setWindowIcon(QIcon(*(g_pIconManager->getSmallIcon(KVI_SMALLICON_ICONMANAGER))));
384
 
 
385
 
        int rows = KVI_NUM_SMALL_ICONS / 10;
386
 
        if((rows * 10) < KVI_NUM_SMALL_ICONS)rows++;
387
 
        QGridLayout * g = new QGridLayout(this);
388
 
        int i;
389
 
        for(i = 0;i < 10;i++)
390
 
        {
391
 
                KviStr tmp(KviStr::Format,"%d",i);
392
 
                QLabel * l = new QLabel(tmp.ptr(),this);
393
 
                g->addWidget(l,0,i + 1);
394
 
        }
395
 
        for(i = 0;i < rows;i++)
396
 
        {
397
 
                KviStr tmp(KviStr::Format,"%d",i * 10);
398
 
                QLabel * l = new QLabel(tmp.ptr(),this);
399
 
                g->addWidget(l,i + 1,0);
400
 
        }
401
 
        for(i = 0;i < KVI_NUM_SMALL_ICONS;i++)
402
 
        {
403
 
                KviStr tmp(KviStr::Format,"%d",i);
404
 
                QLabel * l = new QLabel(this);
405
 
                l->setObjectName(tmp.ptr());
406
 
                l->setPixmap(*(g_pIconManager->getSmallIcon(i)));
407
 
                g->addWidget(l,(i / 10) + 1,(i % 10) + 1);
408
 
                l->installEventFilter(this);
409
 
                l->setAcceptDrops(true);
410
 
        }
411
 
}
412
 
 
413
 
KviIconWidget::~KviIconWidget()
414
 
{
415
 
}
416
 
 
417
 
void KviIconWidget::closeEvent(QCloseEvent * e)
418
 
{
419
 
        e->ignore();
420
 
        emit closed();
421
 
}
422
 
 
423
 
bool KviIconWidget::eventFilter(QObject * o,QEvent *e)
424
 
{
425
 
        if(e->type() == QEvent::MouseButtonPress)
426
 
        {
427
 
                if(o->inherits("QLabel"))
428
 
                {
429
 
                        KviStr szName = ((QLabel *)o)->objectName();
430
 
                        bool bOk;
431
 
                        int iVal = szName.toInt(&bOk);
432
 
                        if(bOk)emit selected(iVal);
433
 
 
434
 
                        if(parentWidget() && isVisible() && parentWidget()->inherits("KviTalPopupMenu"))
435
 
                        {
436
 
                                parentWidget()->close();
437
 
                                return true;
438
 
                        } else {
439
 
                                if(const QPixmap * pix = ((QLabel *)o)->pixmap())
440
 
                                {
441
 
                                        QDrag *drobj = new QDrag(this);
442
 
                                        QMimeData *mimeData = new QMimeData;
443
 
 
444
 
                                        mimeData->setText(o->objectName());
445
 
                                        mimeData->setImageData(*pix);
446
 
                                        drobj->setMimeData(mimeData);
447
 
                                        drobj->setPixmap(*pix);
448
 
                                }
449
 
                        }
450
 
                }
451
 
        } else if(e->type() == QEvent::DragEnter)
452
 
        {
453
 
                ((QDragEnterEvent *)e)->setAccepted(true);
454
 
                return true;
455
 
        }
456
 
        return QWidget::eventFilter(o,e);
457
 
}
458
 
 
459
 
 
460
 
KviCachedPixmap::KviCachedPixmap(QPixmap * ptr,const QString &szPath)
461
 
{
462
 
        m_szPath = szPath;
463
 
        m_tLastAccess = kvi_unixTime();
464
 
        m_pPixmap = ptr;
465
 
        m_uSize = m_pPixmap->width() * m_pPixmap->height() * (m_pPixmap->depth() / 8);
466
 
}
467
 
 
468
 
KviCachedPixmap::~KviCachedPixmap()
469
 
{
470
 
        delete m_pPixmap;
471
 
}
472
 
 
473
 
void KviCachedPixmap::updateLastAccessTime()
474
 
{
475
 
        m_tLastAccess = kvi_unixTime();
476
 
}
477
 
 
478
 
 
479
 
KviIconManager::KviIconManager()
480
 
{
481
 
        int i=0;
482
 
        for(i=0;i<KVI_NUM_SMALL_ICONS;i++)m_smallIcons[i] = 0;
483
 
 
484
 
        initQResourceBackend();
485
 
        //loadSmallIcons();
486
 
 
487
 
        m_pCachedImages = new KviPointerHashTable<QString,KviCachedPixmap>(21,true);
488
 
        m_pCachedImages->setAutoDelete(true);
489
 
 
490
 
        m_uCacheTotalSize = 0;
491
 
        m_uCacheMaxSize = 1024 * 1024; // 1 MB
492
 
 
493
 
        m_pIconWidget = 0;
494
 
 
495
 
        QString buffer;
496
 
 
497
 
        // Load the userchanstate image
498
 
        g_pApp->findImage(buffer,KVI_USERCHANSTATE_IMAGE_NAME);
499
 
        g_pUserChanStatePixmap = new QPixmap(buffer);
500
 
 
501
 
        g_pApp->findImage(buffer,KVI_ACTIVITYMETER_IMAGE_NAME);
502
 
        g_pActivityMeterPixmap = new QPixmap(buffer);
503
 
 
504
 
        m_pIconNames = 0;
505
 
}
506
 
 
507
 
KviIconManager::~KviIconManager()
508
 
{
509
 
        delete g_pUserChanStatePixmap;
510
 
        delete g_pActivityMeterPixmap;
511
 
 
512
 
        if(m_pIconWidget)delete m_pIconWidget;
513
 
        int i;
514
 
 
515
 
        for(i=0;i<KVI_NUM_SMALL_ICONS;i++)
516
 
        {
517
 
                if(m_smallIcons[i])delete m_smallIcons[i];
518
 
        }
519
 
        delete m_pCachedImages;
520
 
        if(m_pIconNames)delete m_pIconNames;
521
 
}
522
 
 
523
 
void KviIconManager::initQResourceBackend()
524
 
{
525
 
        QString szPath;
526
 
 
527
 
        g_pApp->getLocalKvircDirectory(szPath,KviApp::Pics);
528
 
        QDir::addSearchPath("icons", szPath);
529
 
 
530
 
        g_pApp->getGlobalKvircDirectory(szPath,KviApp::Pics);
531
 
        QDir::addSearchPath("icons", szPath);
532
 
 
533
 
        g_pApp->getLocalKvircDirectory(szPath,KviApp::SmallIcons);
534
 
        QDir::addSearchPath("smallicons", szPath);
535
 
 
536
 
        g_pApp->getGlobalKvircDirectory(szPath,KviApp::SmallIcons);
537
 
        QDir::addSearchPath("smallicons", szPath);
538
 
 
539
 
}
540
 
 
541
 
QString KviIconManager::getSmallIconResourceName(int idx)
542
 
{
543
 
        QString szName("smallicons:kcs_");
544
 
        szName.append(g_szIconNames[idx]);
545
 
        szName.append(".png");
546
 
        return szName;
547
 
}
548
 
 
549
 
const char * KviIconManager::getSmallIconName(int idx)
550
 
{
551
 
        return g_szIconNames[idx];
552
 
}
553
 
 
554
 
int KviIconManager::getSmallIconIdFromName(const QString &szName)
555
 
{
556
 
        if(!m_pIconNames)
557
 
        {
558
 
                m_pIconNames = new KviPointerHashTable<QString,int>(257,false);
559
 
                m_pIconNames->setAutoDelete(true);
560
 
 
561
 
                for(int i=0;i<KVI_NUM_SMALL_ICONS;i++)
562
 
                {
563
 
                        int * pInt = new int;
564
 
                        *pInt = i;
565
 
                        m_pIconNames->replace(QString(g_szIconNames[i]),pInt);
566
 
                }
567
 
        }
568
 
        int * pInt = m_pIconNames->find(szName);
569
 
        if(!pInt)return 0;
570
 
        return *pInt;
571
 
}
572
 
 
573
 
void KviIconManager::showIconWidget()
574
 
{
575
 
        if(!m_pIconWidget)
576
 
        {
577
 
                m_pIconWidget = new KviIconWidget();
578
 
                connect(m_pIconWidget,SIGNAL(closed()),this,SLOT(iconWidgetClosed()));
579
 
        }
580
 
        m_pIconWidget->show();
581
 
        m_pIconWidget->raise();
582
 
        m_pIconWidget->setFocus();
583
 
}
584
 
 
585
 
void KviIconManager::iconWidgetClosed()
586
 
{
587
 
        delete m_pIconWidget;
588
 
        m_pIconWidget = 0;
589
 
}
590
 
 
591
 
/*
592
 
        @doc: image_id
593
 
        @title:
594
 
                The image identifier
595
 
        @keyterms:
596
 
                image identifier, using builtin images, how kvirc locates image files
597
 
        @type:
598
 
                language
599
 
        @short:
600
 
                Semantics of the <image_id> parameter
601
 
        @body:
602
 
                Some KVIrc commands and functions accept
603
 
                the <image_id> as a parameter.
604
 
                The <image_id> indicates an image to be displayed
605
 
                by some gui element. Since KVIrc has a set of
606
 
                builtin icons, we want the user to be able to use it :).[br]
607
 
                The <image_id> can be either a signed integer number
608
 
                or a filename.[br]
609
 
                If it is a signed integer it is interpreted as
610
 
                index of the internal KVIrc image to use:
611
 
                positive integers (starting at 0) indicate
612
 
                small (16x16) icons (the ones used in the view widget).[br]
613
 
                If <image_id> is anything else than a signed integer,
614
 
                it is interpreted as a filename.
615
 
                The filename can be an absolute file path or a relative one.
616
 
                In this last case KVIrc will look for the file in a predefined set
617
 
                of directories: First in the local "pics" directory (usually
618
 
                in ~/kvirc-version/pics/), then in the local "incoming" directory,
619
 
                then in the global "pics" directory , then in the user home (~)
620
 
                and in the current directory.[br]
621
 
*/
622
 
 
623
 
KviCachedPixmap * KviIconManager::getPixmapWithCache(const QString &szName)
624
 
{
625
 
        if(szName.isEmpty())return 0;
626
 
 
627
 
        KviCachedPixmap * p = m_pCachedImages->find(szName);
628
 
 
629
 
        if(p)
630
 
        {
631
 
                p->updateLastAccessTime();
632
 
                return p;
633
 
        }
634
 
 
635
 
        QPixmap * pix = 0;
636
 
 
637
 
        QString szRetPath;
638
 
 
639
 
        if(g_pApp->findImage(szRetPath,szName))
640
 
        {
641
 
                pix = new QPixmap(szRetPath);
642
 
                if(pix->isNull())
643
 
                {
644
 
                        delete pix; // it is not an valid image!!! (really bad situation...)
645
 
                        pix = 0;
646
 
                        return 0;
647
 
                }
648
 
        } else {
649
 
                return 0;
650
 
        }
651
 
 
652
 
        p = new KviCachedPixmap(pix,QString(szRetPath));
653
 
        addToCache(szName,p);
654
 
 
655
 
        return p;
656
 
}
657
 
 
658
 
KviCachedPixmap * KviIconManager::getPixmapWithCacheScaleOnLoad(const QString &szName,int iMaxWidth,int iMaxHeight)
659
 
{
660
 
        if(szName.isEmpty())return 0;
661
 
 
662
 
        KviCachedPixmap * p = m_pCachedImages->find(szName);
663
 
 
664
 
        if(p)
665
 
        {
666
 
                p->updateLastAccessTime();
667
 
                return p;
668
 
        }
669
 
 
670
 
        QPixmap * pix = 0;
671
 
 
672
 
        QString szRetPath;
673
 
 
674
 
        if(g_pApp->findImage(szRetPath,szName))
675
 
        {
676
 
                pix = new QPixmap(szRetPath);
677
 
                if(pix->isNull())
678
 
                {
679
 
                        delete pix; // it is not an valid image!!! (really bad situation...)
680
 
                        pix = 0;
681
 
                        return 0;
682
 
                }
683
 
                if((pix->width() > iMaxWidth) || (pix->height() > iMaxHeight))
684
 
                {
685
 
                        // scale to fit
686
 
                        int scaleW = iMaxWidth;
687
 
                        int scaleH;
688
 
                        scaleH = (pix->height() * iMaxWidth) / pix->width();
689
 
                        if(scaleH > iMaxHeight)
690
 
                        {
691
 
                                scaleH = iMaxHeight;
692
 
                                scaleW = (scaleH * pix->width()) / pix->height();
693
 
                        }
694
 
 
695
 
                        QImage img = pix->toImage();
696
 
                        *pix = QPixmap::fromImage(img.scaled(scaleW,scaleH,Qt::IgnoreAspectRatio,Qt::SmoothTransformation));
697
 
                }
698
 
        } else {
699
 
                return 0;
700
 
        }
701
 
 
702
 
        p = new KviCachedPixmap(pix,QString(szRetPath));
703
 
        addToCache(szName,p);
704
 
 
705
 
        return p;
706
 
}
707
 
 
708
 
void KviIconManager::addToCache(const QString &szName,KviCachedPixmap * p)
709
 
{
710
 
        if((m_uCacheTotalSize + p->size()) >= m_uCacheMaxSize)cacheCleanup();
711
 
 
712
 
        m_pCachedImages->insert(szName,p);
713
 
        m_uCacheTotalSize += p->size();
714
 
}
715
 
 
716
 
QPixmap * KviIconManager::getImage(const QString &id,bool bCanBeNumber,QString* pRetPath)
717
 
{
718
 
        if(id.isEmpty())
719
 
                return NULL;
720
 
 
721
 
        if(bCanBeNumber)
722
 
        {
723
 
                bool bOk;
724
 
                int idx = id.toInt(&bOk);
725
 
                if(bOk)
726
 
                {
727
 
                        // was a number : this is not a filename
728
 
                        if(idx >= 0)
729
 
                                return getSmallIcon(idx % KVI_NUM_SMALL_ICONS);
730
 
                } else {
731
 
                        if(id.startsWith("$icon"))
732
 
                        {
733
 
                                QString szTmp = id.trimmed();
734
 
                                szTmp.replace("$icon(","");
735
 
                                szTmp.replace(")","");
736
 
                                szTmp = szTmp.trimmed();
737
 
                                szTmp.replace("\"","");
738
 
                                idx = getSmallIconIdFromName(szTmp.trimmed());
739
 
                                if(idx >= 0)
740
 
                                        return getSmallIcon(idx % KVI_NUM_SMALL_ICONS);
741
 
                                
742
 
                        }
743
 
                }
744
 
        }
745
 
 
746
 
        KviCachedPixmap * p = getPixmapWithCache(id);
747
 
        if(!p)
748
 
                return NULL;
749
 
 
750
 
        if(pRetPath)
751
 
                *pRetPath = p->path();
752
 
 
753
 
        return p->pixmap();
754
 
}
755
 
 
756
 
 
757
 
QPixmap * KviIconManager::getBigIcon(const QString &szName)
758
 
{
759
 
        QPixmap * p = getPixmap(szName);
760
 
        if(p)return p;
761
 
 
762
 
        bool bOk;
763
 
        int idx = szName.toInt(&bOk);
764
 
        if(bOk && (idx >= 0))
765
 
        {
766
 
                // was a number : this is not a filename
767
 
                // it was a small icon: scale it and cache it
768
 
                QString tmpName = szName;
769
 
                tmpName += ".scaled16to32";
770
 
                p = getPixmap(tmpName);
771
 
                if(p)return p;
772
 
                p = getSmallIcon(idx % KVI_NUM_SMALL_ICONS);
773
 
                if(p)
774
 
                {
775
 
                        QImage tmpi = p->toImage();
776
 
                        QImage tmp2 = tmpi.scaled(32,32,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
777
 
                        QPixmap *pix=new QPixmap();
778
 
                        *pix = QPixmap::fromImage(tmp2);
779
 
                        KviCachedPixmap * cp = new KviCachedPixmap(pix,QString());
780
 
                        addToCache(tmpName,cp);
781
 
                        return cp->pixmap();
782
 
                }
783
 
        }
784
 
 
785
 
        p = getPixmap("kvi_bigicon_unknown.png");
786
 
        if(p)return p;
787
 
        KviCachedPixmap * cp = new KviCachedPixmap(new QPixmap(32,32),QString());
788
 
        addToCache(szName,cp);
789
 
        return cp->pixmap();
790
 
}
791
 
 
792
 
void KviIconManager::urlToCachedFileName(QString &szFName)
793
 
{
794
 
        szFName.replace(":","_");
795
 
        szFName.replace(";","_");
796
 
        szFName.replace("/","_");
797
 
        szFName.replace("\\","_");
798
 
        szFName.replace("\"","_");
799
 
        szFName.replace("\'","_");
800
 
        szFName.replace("(","_");
801
 
        szFName.replace(")","_");
802
 
        szFName.replace("?","_");
803
 
        szFName.replace("___",".");
804
 
        //cut filenames to 255 chars, trying to preserve file name/extension (bug #616)
805
 
        if(szFName.size()>255)
806
 
        {
807
 
                QString ext = szFName.right(55);
808
 
                szFName.truncate(200);
809
 
                szFName.append(ext);
810
 
        }
811
 
}
812
 
 
813
 
KviAvatar * KviIconManager::getAvatar(const QString &szLocalPath,const QString &szName)
814
 
{
815
 
        QString szP,szN;
816
 
 
817
 
        if(szLocalPath.isEmpty())
818
 
        {
819
 
                //both path and name empty => fail
820
 
                if(szName.isEmpty())
821
 
                        return 0;
822
 
                if(KviQString::equalCIN("http://",szName,7))
823
 
                {
824
 
                        // it's an url, cache it locally
825
 
                        szN = szName;
826
 
                        urlToCachedFileName(szN);
827
 
                        g_pApp->getLocalKvircDirectory(szP,KviApp::Avatars,szN);
828
 
                        szN = szName; // keep the remote url as name
829
 
                } else {
830
 
                        //locally cached avatar, called only by its szName
831
 
                        szN = KviFileUtils::extractFileName(szName);
832
 
                        g_pApp->getLocalKvircDirectory(szP,KviApp::Avatars,szN);
833
 
                }
834
 
        } else if(szName.isEmpty())
835
 
        {
836
 
                if(szLocalPath.isEmpty())
837
 
                        return 0;
838
 
                szP = szLocalPath;
839
 
                szN = KviFileUtils::extractFileName(szLocalPath);
840
 
        } else {
841
 
                szP = szLocalPath;
842
 
                szN = szName;
843
 
        }
844
 
 
845
 
        // avatars bigger than 1024x768 just sux: they can't be seen on tooltips anyway
846
 
        //KviCachedPixmap * p = KVI_OPTION_BOOL(KviOption_boolScaleAvatarsOnLoad) ? getPixmapWithCacheScaleOnLoad(szP,
847
 
        //      KVI_OPTION_UINT(KviOption_uintScaleAvatarsOnLoadWidth)
848
 
        //      ,KVI_OPTION_UINT(KviOption_uintScaleAvatarsOnLoadHeight)) : getPixmapWithCache(szP);
849
 
        //if(!p)return 0;
850
 
 
851
 
 
852
 
        KviAvatar* result = 0;
853
 
 
854
 
        if(KviFileUtils::fileExists(szP))
855
 
        {
856
 
                if(KVI_OPTION_BOOL(KviOption_boolScaleAvatarsOnLoad))
857
 
                {
858
 
                        result = new KviAvatar(szP,szN,
859
 
                                                QSize(
860
 
                                                        KVI_OPTION_UINT(KviOption_uintScaleAvatarsOnLoadWidth),
861
 
                                                        KVI_OPTION_UINT(KviOption_uintScaleAvatarsOnLoadHeight)
862
 
                                                )
863
 
                                        );
864
 
                } else {
865
 
                        result = new KviAvatar(szP,szN);
866
 
                }
867
 
        }
868
 
 
869
 
        //Can't load it
870
 
        if(result && !result->isValid())
871
 
        {
872
 
                delete result;
873
 
                result = 0;
874
 
        }
875
 
 
876
 
        return result;
877
 
}
878
 
 
879
 
void KviIconManager::clearCache()
880
 
{
881
 
        m_pCachedImages->clear();
882
 
}
883
 
 
884
 
void KviIconManager::reloadImages()
885
 
{
886
 
        clearCache();
887
 
        for(int i=0;i<KVI_NUM_SMALL_ICONS;i++)
888
 
        {
889
 
                if(m_smallIcons[i])delete m_smallIcons[i];
890
 
                m_smallIcons[i] = 0;
891
 
        }
892
 
}
893
 
 
894
 
 
895
 
QPixmap * KviIconManager::loadSmallIcon(int idx)
896
 
{
897
 
        if(idx >= KVI_NUM_SMALL_ICONS)return 0;
898
 
        if(idx < 0)return 0;
899
 
 
900
 
        QString szPath;
901
 
        QString buffer;
902
 
        KviQString::sprintf(szPath,KVI_SMALLICONS_PREFIX "%s.png",g_szIconNames[idx]);
903
 
 
904
 
        g_pApp->findSmallIcon(buffer,szPath);
905
 
        m_smallIcons[idx] = new QPixmap(buffer);
906
 
 
907
 
        //if(m_smallIcon[idx]->isNull())
908
 
        //{
909
 
        // // load an "unknown" image ?.. but should never happen
910
 
        //}
911
 
        return m_smallIcons[idx];
912
 
}
913
 
 
914
 
void KviIconManager::cacheCleanup()
915
 
{
916
 
        QStringList l;
917
 
 
918
 
        KviPointerHashTableIterator<QString,KviCachedPixmap> it(*m_pCachedImages);
919
 
 
920
 
 
921
 
//#warning "IMPLEMENT CLEANUP"
922
 
/*
923
 
        while(it.current())
924
 
        {
925
 
                kvi_time_t curTime = kvi_unixTime();
926
 
 
927
 
                if((((unsigned int)it.current()->timestamp) - ((unsigned int)curTime)) > 120)
928
 
                {
929
 
                        // unused since 2 minutes
930
 
                        // time to cleanup it...
931
 
                        l.append(new KviStr(it.currentKey()));
932
 
                }
933
 
                ++it;
934
 
        }
935
 
 
936
 
        for(QStringList::Iterator it = l.begin();it != l.end();++it)
937
 
        {
938
 
                m_pCachedImages->remove(*it);
939
 
        }
940
 
*/
941
 
/*
942
 
        for(KviStr * p =l.first();p;p = l.next())
943
 
        {
944
 
                m_pCachedImages->remove(p->ptr());
945
 
        }
946
 
        if(m_pCachedImages->isEmpty())m_pCleanupTimer->stop();
947
 
*/
948
 
}
949
 
 
950
 
#ifndef COMPILE_USE_STANDALONE_MOC_SOURCES
951
 
#include "kvi_iconmanager.moc"
952
 
#endif //!COMPILE_USE_STANDALONE_MOC_SOURCES