File: chat-style-installer.cpp

package info (click to toggle)
ktp-text-ui 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,680 kB
  • ctags: 1,142
  • sloc: cpp: 9,451; sh: 15; makefile: 9
file content (173 lines) | stat: -rw-r--r-- 6,032 bytes parent folder | download
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
/*
    KDE Telepathy AdiumxtraProtocolHandler - Install Adiumxtra packages through adiumxtra://-pseudo protocol
    Copyright (C) 2010 Dominik Schmidt <domme@rautelinux.org>

    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.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "chat-style-installer.h"

#include "chat-window-style-manager.h"
#include "chat-style-plist-file-reader.h"

#include <KDebug>
#include <KTemporaryFile>
#include <KArchiveFile>
#include <KLocale>
#include <KNotification>
#include <KApplication>
#include <QTimer>
#include <KAboutData>
#include <KComponentData>

// FIXME: Part of a hack to let adiumxtra-protocol-handler use the main ktelepathy.notifyrc because
// the string freeze does not permit adding a new notifyrc only for adiumxtra-protocol-handler.
// Remove this after 0.7 is released.
static KComponentData ktelepathyComponentData() {
    KAboutData telepathySharedAboutData("ktelepathy",0,KLocalizedString(),0);
    return KComponentData(telepathySharedAboutData);
}

ChatStyleInstaller::ChatStyleInstaller(KArchive *archive, KTemporaryFile *tmpFile)
{
    kDebug();

    m_archive = archive;
    m_tmpFile = tmpFile;
}

ChatStyleInstaller::~ChatStyleInstaller()
{
    kDebug();
}

BundleInstaller::BundleStatus ChatStyleInstaller::validate()
{
    kDebug();

    KArchiveEntry *currentEntry = 0L;
    KArchiveDirectory* currentDir = 0L;
    const KArchiveDirectory* rootDir = m_archive->directory();
    int validResult = 0;
    const QStringList entries = rootDir->entries();
    QStringList::ConstIterator entriesIt;

    for (entriesIt = entries.begin(); entriesIt != entries.end(); ++entriesIt) {
        currentEntry = const_cast<KArchiveEntry*>(rootDir->entry(*entriesIt));
        kDebug() << "Current entry name: " << currentEntry->name();
        if (currentEntry->isDirectory()) {
            currentDir = dynamic_cast<KArchiveDirectory*>(currentEntry);
            if (currentDir) {
                if (currentDir->entry(QLatin1String("Contents")) &&
                    currentDir->entry(QLatin1String("Contents"))->isDirectory()) {
                    kDebug() << "Contents found";
                    validResult += 1;
                }
                if (currentDir->entry(QLatin1String("Contents/Info.plist")) &&
                    currentDir->entry(QLatin1String("Contents/Info.plist"))->isFile()) {
                    kDebug() << "Contents/Info.plist found";
                    KArchiveFile const *info = dynamic_cast<KArchiveFile const *>(
                        currentDir->entry(QLatin1String("Contents/Info.plist"))
                    );
                    QByteArray data = info->data();
                    ChatStylePlistFileReader reader(data);
                    if(m_bundleName.isEmpty()) {
                        m_bundleName = reader.CFBundleName();
                    }
                    validResult += 1;
                }
            }
        }
    }

    if(validResult >= 2) {
        kDebug() << "Bundle is valid";
        return BundleValid;
    } else {
        kDebug() << "Bundle is not valid";
        return BundleNotValid;
    }
}

QString ChatStyleInstaller::bundleName() const
{
    kDebug();

    return m_bundleName;
}

BundleInstaller::BundleStatus ChatStyleInstaller::install()
{
    kDebug();

    BundleInstaller::BundleStatus status = static_cast<BundleInstaller::BundleStatus>(
        ChatWindowStyleManager::self()->installStyle(m_archive->fileName())
    );
    kDebug()<< "status " << status;
    delete m_tmpFile;

    m_status = status;

    Q_EMIT finished(status);

    return status;
}

void ChatStyleInstaller::showRequest()
{
    kDebug();

    KNotification *notification = new KNotification(QLatin1String("chatstyleRequest"), NULL, KNotification::Persistent);
    notification->setText( i18n("Install Chatstyle %1", this->bundleName()) );
    notification->setActions( QStringList() << i18n("Install") << i18n("Cancel") );

    QObject::connect(notification, SIGNAL(action1Activated()), this, SLOT(install()));
    QObject::connect(notification, SIGNAL(action1Activated()), notification, SLOT(close()));

    QObject::connect(notification, SIGNAL(ignored()), this, SLOT(ignoreRequest()));
    QObject::connect(notification, SIGNAL(ignored()), notification, SLOT(close()));

    QObject::connect(notification, SIGNAL(action2Activated()), this, SLOT(ignoreRequest()));
    QObject::connect(notification, SIGNAL(action2Activated()), notification, SLOT(close()));

    notification->setComponentData(ktelepathyComponentData());
    notification->sendEvent();
}

void ChatStyleInstaller::showResult()
{
    kDebug();

    KNotification *notification;
    if(m_status == BundleInstaller::BundleInstallOk) {
        kDebug() << "Installed Chatstyle" << this->bundleName() << "successfully";
        notification = new KNotification(QLatin1String("chatstyleSuccess"));
        notification->setText( i18n("Installed Chatstyle %1 successfully.", this->bundleName()) );
    } else {
        kDebug() << "Installation of Chatstyle" << this->bundleName() << "failed";
        notification = new KNotification(QLatin1String("chatstyleFailure"));
        notification->setText( i18n("Installation of Chatstyle %1 failed.", this->bundleName()) );
    }
    notification->setComponentData(ktelepathyComponentData());
    notification->sendEvent();

    Q_EMIT showedResult();
}

void ChatStyleInstaller::ignoreRequest()
{
    kDebug();

    Q_EMIT ignoredRequest();
}