1 /*
2     KDE Telepathy AdiumxtraProtocolHandler - Install Adiumxtra packages through adiumxtra://-pseudo protocol
3     Copyright (C) 2010 Dominik Schmidt <domme@rautelinux.org>
4 
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 2 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "emoticon-set-installer.h"
20 
21 #include "chat-style-plist-file-reader.h"
22 
23 #include <QTemporaryFile>
24 
25 #include <KArchiveFile>
26 #include <KEmoticons>
27 #include <KArchiveDirectory>
28 #include <KNotification>
29 #include <KLocalizedString>
30 
31 // FIXME: Part of a hack to let adiumxtra-protocol-handler use the main ktelepathy.notifyrc because
32 // the string freeze does not permit adding a new notifyrc only for adiumxtra-protocol-handler.
33 // Remove this after 0.7 is released.
ktelepathyComponentName()34 static QString ktelepathyComponentName() {
35     return QStringLiteral("ktelepathy");
36 }
37 
EmoticonSetInstaller(KArchive * archive,QTemporaryFile * tmpFile)38 EmoticonSetInstaller::EmoticonSetInstaller(KArchive *archive, QTemporaryFile *tmpFile)
39 {
40     m_archive = archive;
41     m_tmpFile = tmpFile;
42 }
43 
~EmoticonSetInstaller()44 EmoticonSetInstaller::~EmoticonSetInstaller()
45 {
46 }
47 
validate()48 BundleInstaller::BundleStatus EmoticonSetInstaller::validate()
49 {
50     KArchiveEntry *currentEntry = 0L;
51     KArchiveDirectory* currentDir = 0L;
52     if(m_archive == 0) exit(1);
53     m_archive->fileName();
54     m_archive->directory();
55     const KArchiveDirectory* rootDir = m_archive->directory();
56     const QStringList entries = rootDir->entries();
57     // Will be reused later.
58     QStringList::ConstIterator entriesIt;
59     for (entriesIt = entries.begin(); entriesIt != entries.end(); ++entriesIt) {
60         currentEntry = const_cast<KArchiveEntry*>(rootDir->entry(*entriesIt));
61         if (currentEntry->isDirectory()) {
62             currentDir = dynamic_cast<KArchiveDirectory*>(currentEntry);
63             if (currentDir) {
64                 if (currentDir->entry(QString::fromUtf8("Emoticons.plist"))) {
65                    QString currentItem = currentEntry->name();
66                    if(m_bundleName.isEmpty() && currentItem.endsWith(QLatin1String(".AdiumEmoticonset"))) {
67                        m_bundleName = currentItem.remove(QLatin1String(".AdiumEmoticonset"));
68                    }
69                    return BundleValid;
70                 }
71             }
72         }
73     }
74 
75     return BundleNotValid;
76 }
77 
bundleName() const78 QString EmoticonSetInstaller::bundleName() const
79 {
80     return m_bundleName;
81 }
82 
install()83 BundleInstaller::BundleStatus EmoticonSetInstaller::install()
84 {
85     KEmoticons emoticons;
86     emoticons.installTheme(m_tmpFile->fileName());
87 
88     // we trust in KEmoticons as it gives us no status information
89     // installTheme only returns a list of installed themes if we compare the list before and after
90     // the style could have been updated and the list would not have changed
91     Q_EMIT finished(BundleInstallOk);
92     return BundleInstallOk;
93 }
94 
showRequest()95 void EmoticonSetInstaller::showRequest()
96 {
97     KNotification *notification = new KNotification(QLatin1String("emoticonsRequest"), NULL, KNotification::Persistent);
98     notification->setText( i18n("Install Emoticonset %1", this->bundleName()) );
99     notification->setActions( QStringList() << i18n("Install") << i18n("Cancel") );
100 
101     QObject::connect(notification, SIGNAL(action1Activated()), this, SLOT(install()));
102     QObject::connect(notification, SIGNAL(action1Activated()), notification, SLOT(close()));
103 
104     QObject::connect(notification, SIGNAL(ignored()), this, SLOT(ignoreRequest()));
105     QObject::connect(notification, SIGNAL(ignored()), notification, SLOT(close()));
106 
107     QObject::connect(notification, SIGNAL(action2Activated()), this, SLOT(ignoreRequest()));
108     QObject::connect(notification, SIGNAL(action2Activated()), notification, SLOT(close()));
109 
110     notification->setComponentName(ktelepathyComponentName());
111     notification->sendEvent();
112 }
113 
showResult()114 void EmoticonSetInstaller::showResult()
115 {
116     KNotification *notification = new KNotification(QLatin1String("emoticonsSuccess"));
117     notification->setText( i18n("Installed Emoticonset %1 successfully.", this->bundleName()) );
118     notification->setComponentName(ktelepathyComponentName());
119     notification->sendEvent();
120 
121     Q_EMIT showedResult();
122 }
123 
ignoreRequest()124 void EmoticonSetInstaller::ignoreRequest()
125 {
126     Q_EMIT ignoredRequest();
127 }
128 
129 #include "emoticon-set-installer.moc"
130