~ubuntu-branches/ubuntu/vivid/psi/vivid

« back to all changes in this revision

Viewing changes to src/tools/iconset/iconset.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-08-28 18:46:52 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080828184652-iiik12dl91nq7cdi
Tags: 0.12-2
Uploading to unstable (Closes: Bug#494352)

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
#include "iconset.h"
22
 
#include "zip.h"
23
22
 
24
23
#include <QObject>
25
24
#include <QFile>
32
31
#include <QDomDocument>
33
32
#include <QThread>
34
33
#include <QCoreApplication>
 
34
#include <QTextCodec>
35
35
 
36
36
#include "anim.h"
37
37
 
45
45
#       include <qca_basic.h>
46
46
#endif
47
47
 
 
48
#ifndef NO_ICONSET_ZIP
 
49
#define ICONSET_ZIP
 
50
#endif
 
51
 
 
52
#ifdef ICONSET_ZIP
 
53
#       include "zip.h"
 
54
#endif
 
55
 
48
56
#include <QApplication>
49
57
#include <Q3Shared> // TODO FIXME: port all Q3Shared classes to QSharedData
50
58
 
289
297
public:
290
298
        QString name;
291
299
        QRegExp regExp;
292
 
        QHash<QString, QString> text;
 
300
        QList<IconText> text;
293
301
        QString sound;
294
302
 
295
303
        Impix impix;
441
449
                detach();
442
450
 
443
451
        d->impix = impix;
 
452
        if ( d->icon ) {
 
453
                delete d->icon;
 
454
                d->icon = 0;
 
455
        }
 
456
 
444
457
        emit d->pixmapChanged();
445
458
        emit d->iconModified();
446
459
}
563
576
 * \sa setText()
564
577
 * \sa regExp()
565
578
 */
566
 
const QHash<QString, QString> &PsiIcon::text() const
 
579
const QList<PsiIcon::IconText> &PsiIcon::text() const
567
580
{
568
581
        return d->text;
569
582
}
572
585
 * Sets the PsiIcon text to \a t.
573
586
 * \sa text()
574
587
 */
575
 
void PsiIcon::setText(const QHash<QString, QString> &t)
 
588
void PsiIcon::setText(const QList<PsiIcon::IconText> &t)
576
589
{
577
590
        detach();
578
591
 
580
593
}
581
594
 
582
595
/**
 
596
 * Returns default icon text that could be used as a default value
 
597
 * for e.g. emoticon selector.
 
598
 */
 
599
QString PsiIcon::defaultText() const
 
600
{
 
601
        if (text().isEmpty())
 
602
                return QString();
 
603
 
 
604
        // first, try to get the text by priorities
 
605
        QStringList lang;
 
606
        lang << QString(QTextCodec::locale()).left(2); // most prioritent, is the local language
 
607
        lang << "";                                    // and then the language without name goes (international?)
 
608
        lang << "en";                                  // then real English
 
609
 
 
610
        QString str;
 
611
        QStringList::Iterator it = lang.begin();
 
612
        for (; it != lang.end(); ++it) {
 
613
                foreach(IconText t, text()){
 
614
                        if (t.lang == *it) {
 
615
                                str = t.text;
 
616
                                break;
 
617
                        }
 
618
                }
 
619
        }
 
620
 
 
621
        // if all fails, just get the first text
 
622
        if (str.isEmpty()) {
 
623
                foreach(IconText t, text()){
 
624
                        if (!t.text.isEmpty()) {
 
625
                                str = t.text;
 
626
                                break;
 
627
                        }
 
628
                }
 
629
        }
 
630
 
 
631
        return str;
 
632
}
 
633
 
 
634
/**
583
635
 * Returns file name of associated sound.
584
636
 * \sa setSound()
585
637
 * \sa activated()
961
1013
                QFileInfo fi(dir);
962
1014
                if ( fi.isDir() ) {
963
1015
                        QFile file ( dir + "/" + fileName );
964
 
                        file.open (IO_ReadOnly);
 
1016
                        if (!file.open(IO_ReadOnly))
 
1017
                                return ba;
965
1018
 
966
1019
                        ba = file.readAll();
967
1020
                }
 
1021
#ifdef ICONSET_ZIP
968
1022
                else if ( fi.extension(false) == "jisp" || fi.extension(false) == "zip" ) {
969
1023
                        UnZip z(dir);
970
1024
                        if ( !z.open() )
976
1030
                                z.readFile(n, &ba);
977
1031
                        }
978
1032
                }
 
1033
#endif
979
1034
 
980
1035
                return ba;
981
1036
        }
1033
1088
                PsiIcon icon;
1034
1089
                icon.blockSignals(true);
1035
1090
 
1036
 
                QHash<QString, QString> text, graphic, sound, object;
 
1091
                QList<PsiIcon::IconText> text;
 
1092
                QHash<QString, QString> graphic, sound, object;
1037
1093
 
1038
1094
                QString name;
1039
1095
                name.sprintf("icon_%04d", icon_counter++);
1050
1106
                                QString lang = e.attribute("xml:lang");
1051
1107
                                if ( lang.isEmpty() )
1052
1108
                                        lang = ""; // otherwise there would be many warnings :-(
1053
 
                                text.insertMulti(lang, e.text());
 
1109
                                QString t = e.text();
 
1110
                                if (!t.isEmpty()) {
 
1111
                                        text.append(PsiIcon::IconText(lang, t));
 
1112
                                }
1054
1113
                        }
1055
1114
                        else if ( tag == "object" ) {
1056
1115
                                object[e.attribute("mime")] = e.text();
1131
1190
                                                        }
1132
1191
                                        }
1133
1192
 
1134
 
                                        if ( icon.loadFromData( loadData(graphic[*it], dir), isAnimated ) ) {
 
1193
                                        QByteArray ba = loadData(graphic[*it], dir);
 
1194
 
 
1195
                                        if ( icon.loadFromData( ba, isAnimated ) ) {
1135
1196
                                                loadSuccess = true;
1136
1197
                                                break;
1137
1198
                                        }
1138
1199
                                        else {
1139
 
                                                qDebug("Iconset::load(): Couldn't load %s graphic for the %s icon for the %s iconset", (*it).latin1(), name.latin1(), this->name.latin1());
 
1200
                                                qDebug("Iconset::load(): Couldn't load %s (%s) graphic for the %s icon for the %s iconset", (*it).toLatin1().data(), graphic[*it].toLatin1().data(), name.toLatin1().data(), this->name.latin1());
1140
1201
                                                loadSuccess = false;
1141
1202
                                        }
1142
1203
                                }
1157
1218
                                                if ( path.isEmpty() )
1158
1219
                                                        break;
1159
1220
 
1160
 
                                                if( !QCA::isSupported("sha1") )
1161
 
                                                        continue;
1162
 
 
1163
1221
                                                QFileInfo ext(sound[*it]);
1164
1222
                                                path += "/" + QCA::Hash("sha1").hashToString(QString(fi.absFilePath() + "/" + *sound[*it]).utf8()) + "." + ext.extension();
1165
1223
 
1182
1240
                        }
1183
1241
                }
1184
1242
 
1185
 
                if ( text.count() )
1186
 
                {       // construct RegExp
1187
 
                        QString regexp;
1188
 
                        
1189
 
                        QHashIterator<QString, QString> it( text);
1190
 
                        while ( it.hasNext() ) {
1191
 
                                it.next();
1192
 
                                
1193
 
                                if ( !regexp.isEmpty() )
1194
 
                                        regexp += '|';
1195
 
 
1196
 
                                regexp += QRegExp::escape(it.value());
 
1243
                // construct RegExp
 
1244
                if ( text.count() ) {
 
1245
                        QStringList regexp;
 
1246
                        foreach(PsiIcon::IconText t, text) {
 
1247
                                regexp += QRegExp::escape(t.text);
1197
1248
                        }
1198
 
                        
 
1249
 
1199
1250
                        // make sure there is some form of whitespace on at least one side of the text string
1200
1251
                        //regexp = QString("(\\b(%1))|((%2)\\b)").arg(regexp).arg(regexp);
1201
 
                        icon.setRegExp ( QRegExp(regexp) );
 
1252
                        icon.setRegExp ( QRegExp(regexp.join("|")) );
1202
1253
                }
1203
1254
 
1204
1255
                icon.blockSignals(false);