File: adiumxtra-protocol-handler.cpp

package info (click to toggle)
ktp-text-ui 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,788 kB
  • sloc: cpp: 6,669; sh: 11; makefile: 6
file content (163 lines) | stat: -rw-r--r-- 6,723 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
/*
    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 "adiumxtra-protocol-handler.h"
#include "chat-style-installer.h"
#include "emoticon-set-installer.h"

#include <KDebug>
#include <KZip>
#include <KTar>
#include <KEmoticons>
#include <KTemporaryFile>
#include <KIO/Job>
#include <KIO/NetAccess>
#include <KNotification>
#include <KIcon>
#include <KMimeType>

AdiumxtraProtocolHandler::AdiumxtraProtocolHandler()
    : QObject()
{
    kDebug();
}

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

void AdiumxtraProtocolHandler::install()
{
    kDebug();

    if (m_url.isEmpty()) {
        Q_EMIT finished();
        return; // BundleInstaller:: xxxxx
    }

    KUrl url(m_url);
    if(url.protocol() == QLatin1String("adiumxtra")) {
        url.setProtocol(QLatin1String("http"));
    }

    KTemporaryFile *tmpFile = new KTemporaryFile();
    if (tmpFile->open()) {
        KIO::Job* getJob = KIO::file_copy(url.prettyUrl(), KUrl(tmpFile->fileName()), -1, KIO::Overwrite);
        if (!KIO::NetAccess::synchronousRun(getJob, 0)) {
            kDebug() << "Download failed";
            Q_EMIT finished();
            return; // BundleInstaller::BundleCannotOpen;
        }
        getJob->deleteLater();
    }

    KArchive *archive = 0L;

    QString currentBundleMimeType = KMimeType::findByPath(tmpFile->fileName(), 0, false)->name();
    if (currentBundleMimeType == QLatin1String("application/zip")) {
        archive = new KZip(tmpFile->fileName());
    } else if (currentBundleMimeType == QLatin1String("application/x-compressed-tar") ||
               currentBundleMimeType == QLatin1String("application/x-bzip-compressed-tar") ||
               currentBundleMimeType == QLatin1String("application/x-gzip") ||
               currentBundleMimeType == QLatin1String("application/x-bzip")) {
        archive = new KTar(tmpFile->fileName());
    } else {
        KNotification *notification = new KNotification(QLatin1String("packagenotrecognized"), NULL, KNotification::Persistent);
        notification->setText( i18n("Package type not recognized or not supported") );
        notification->setActions( QStringList() << i18n("OK") );
        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()));
        notification->sendEvent();
        kDebug() << "Unsupported file type" << currentBundleMimeType;
        kDebug() << tmpFile->fileName();
        Q_EMIT finished();
        return;// BundleInstaller::BundleNotValid;
    }

    if (!archive->open(QIODevice::ReadOnly)) {
        delete archive;
        kDebug() << "Cannot open theme file";
        Q_EMIT finished();
        return;// BundleInstaller::BundleCannotOpen;
    }

    ChatStyleInstaller chatStyleInstaller(archive, tmpFile);
    if (chatStyleInstaller.validate() == BundleInstaller::BundleValid) {
        chatStyleInstaller.showRequest();
        kDebug() << "Sent messagestyle request";

        QObject::connect(&chatStyleInstaller, SIGNAL(finished(BundleInstaller::BundleStatus)),
                         &chatStyleInstaller, SLOT(showResult()));
        QObject::connect(&chatStyleInstaller, SIGNAL(showedResult()), this, SIGNAL(finished()));
        QObject::connect(&chatStyleInstaller, SIGNAL(showedResult()),
                         &chatStyleInstaller, SLOT(deleteLater()));
        QObject::connect(&chatStyleInstaller, SIGNAL(ignoredRequest()), this, SIGNAL(finished()));
        QObject::connect(&chatStyleInstaller, SIGNAL(ignoredRequest()),
                         &chatStyleInstaller, SLOT(deleteLater()));

        kDebug() << "Starting installation";
        chatStyleInstaller.install();

        return;// BundleInstaller::BundleValid;
    }

    EmoticonSetInstaller emoticonSetInstaller(archive, tmpFile);
    if(emoticonSetInstaller.validate() == BundleInstaller::BundleValid) {
        emoticonSetInstaller.showRequest();
        kDebug() << "Sent emoticonset request";

        QObject::connect(&emoticonSetInstaller, SIGNAL(finished(BundleInstaller::BundleStatus)),
                         &emoticonSetInstaller, SLOT(showResult()));
        QObject::connect(&emoticonSetInstaller, SIGNAL(showedResult()), this, SIGNAL(finished()));
        QObject::connect(&emoticonSetInstaller, SIGNAL(showedResult()),
                         &emoticonSetInstaller, SLOT(deleteLater()));
        QObject::connect(&emoticonSetInstaller, SIGNAL(ignoredRequest()), this, SIGNAL(finished()));
        QObject::connect(&emoticonSetInstaller, SIGNAL(ignoredRequest()),
                         &emoticonSetInstaller, SLOT(deleteLater()));

        kDebug() << "Starting installation";
        emoticonSetInstaller.install();

        return;// BundleInstaller::BundleValid;
    }

    KNotification *notification = new KNotification(QLatin1String("packagenotrecognized"),
                                                    NULL,
                                                    KNotification::Persistent);
    notification->setText( i18n("Package type not recognized or not supported") );
    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()));
    notification->setActions( QStringList() << i18n("OK") );
    notification->sendEvent();
    kDebug() << "Sent error";

    Q_EMIT finished();
    return;// BundleInstaller::BundleUnknownError;
}

void AdiumxtraProtocolHandler::setUrl(const QString& url)
{
    m_url = url;
}