File: imageplugin.cpp

package info (click to toggle)
psi-plus 1.4.1456-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,188 kB
  • sloc: cpp: 211,254; ansic: 19,786; javascript: 13,687; xml: 4,056; sh: 1,610; makefile: 437; objc: 407; python: 277; ruby: 171
file content (269 lines) | stat: -rw-r--r-- 9,978 bytes parent folder | download | duplicates (2)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * imageplugin.cpp - plugin
 * Copyright (C) 2009-2010  VampiRus
 *
 * 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 <https://www.gnu.org/licenses/>.
 *
 */

#include "accountinfoaccessinghost.h"
#include "accountinfoaccessor.h"
#include "activetabaccessinghost.h"
#include "activetabaccessor.h"
#include "gctoolbariconaccessor.h"
#include "iconfactoryaccessinghost.h"
#include "iconfactoryaccessor.h"
#include "optionaccessinghost.h"
#include "optionaccessor.h"
#include "plugininfoprovider.h"
#include "psiaccountcontroller.h"
#include "psiaccountcontrollinghost.h"
#include "psiplugin.h"
#include "stanzasender.h"
#include "stanzasendinghost.h"
#include "toolbariconaccessor.h"
#include <QApplication>
#include <QByteArray>
#include <QClipboard>
#include <QFile>
#include <QFileDialog>
#include <QLabel>
#include <QMenu>
#include <QMessageBox>
#include <QVBoxLayout>

#define constVersion "0.1.2"

#define CONST_LAST_FOLDER "lastfolder"

const int MAX_SIZE = 400;

class ImagePlugin : public QObject,
                    public PsiPlugin,
                    public ToolbarIconAccessor,
                    public GCToolbarIconAccessor,
                    public StanzaSender,
                    public IconFactoryAccessor,
                    public ActiveTabAccessor,
                    public PluginInfoProvider,
                    public AccountInfoAccessor,
                    public PsiAccountController,
                    public OptionAccessor {
    Q_OBJECT
    Q_PLUGIN_METADATA(IID "com.psi-plus.ImagePlugin" FILE "psiplugin.json")
    Q_INTERFACES(PsiPlugin ToolbarIconAccessor GCToolbarIconAccessor StanzaSender ActiveTabAccessor PsiAccountController
                     OptionAccessor IconFactoryAccessor AccountInfoAccessor PluginInfoProvider)
public:
    ImagePlugin();
    virtual QString  name() const;
    virtual QWidget *options();
    virtual bool     enable();
    virtual bool     disable();

    virtual void                applyOptions() { }
    virtual void                restoreOptions() { }
    virtual QList<QVariantHash> getButtonParam();
    virtual QAction *           getAction(QObject *, int, const QString &) { return nullptr; }
    virtual QList<QVariantHash> getGCButtonParam();
    virtual QAction *           getGCAction(QObject *, int, const QString &) { return nullptr; }
    virtual void                setIconFactoryAccessingHost(IconFactoryAccessingHost *host);
    virtual void                setStanzaSendingHost(StanzaSendingHost *host);
    virtual void                setActiveTabAccessingHost(ActiveTabAccessingHost *host);
    virtual void                setAccountInfoAccessingHost(AccountInfoAccessingHost *host);
    virtual void                setPsiAccountControllingHost(PsiAccountControllingHost *host);
    virtual void                setOptionAccessingHost(OptionAccessingHost *host);
    virtual void                optionChanged(const QString &) { }
    virtual QString             pluginInfo();

private slots:
    void actionActivated();

private:
    IconFactoryAccessingHost * iconHost;
    StanzaSendingHost *        stanzaSender;
    ActiveTabAccessingHost *   activeTab;
    AccountInfoAccessingHost * accInfo;
    PsiAccountControllingHost *psiController;
    OptionAccessingHost *      psiOptions;
    bool                       enabled;
    QHash<QString, int>        accounts_;
};

ImagePlugin::ImagePlugin() :
    iconHost(nullptr), stanzaSender(nullptr), activeTab(nullptr), accInfo(nullptr), psiController(nullptr),
    psiOptions(nullptr), enabled(false)
{
}

QString ImagePlugin::name() const { return "Image Plugin"; }

bool ImagePlugin::enable()
{
    QFile file(":/imageplugin/imageplugin.gif");
    if (file.open(QIODevice::ReadOnly)) {
        QByteArray image = file.readAll();
        iconHost->addIcon("imageplugin/icon", image);
        file.close();
        enabled = true;
    } else {
        enabled = false;
    }
    return enabled;
}

bool ImagePlugin::disable()
{
    enabled = false;
    return true;
}

QWidget *ImagePlugin::options()
{
    if (!enabled) {
        return nullptr;
    }
    QWidget *    optionsWid = new QWidget();
    QVBoxLayout *vbox       = new QVBoxLayout(optionsWid);
    QLabel *     wikiLink
        = new QLabel(tr("<a href=\"https://psi-plus.com/wiki/en:plugins#image_plugin\">Wiki (Online)</a>"), optionsWid);
    wikiLink->setOpenExternalLinks(true);
    vbox->addWidget(wikiLink);
    vbox->addStretch();
    return optionsWid;
}

QList<QVariantHash> ImagePlugin::getButtonParam()
{
    QVariantHash hash;
    hash["tooltip"] = QVariant(tr("Send Image"));
    hash["icon"]    = QVariant(QString("imageplugin/icon"));
    hash["reciver"] = QVariant::fromValue(qobject_cast<QObject *>(this));
    hash["slot"]    = QVariant(SLOT(actionActivated()));
    QList<QVariantHash> l;
    l.push_back(hash);
    return l;
}

QList<QVariantHash> ImagePlugin::getGCButtonParam() { return getButtonParam(); }

void ImagePlugin::setAccountInfoAccessingHost(AccountInfoAccessingHost *host) { accInfo = host; }

void ImagePlugin::setIconFactoryAccessingHost(IconFactoryAccessingHost *host) { iconHost = host; }

void ImagePlugin::setPsiAccountControllingHost(PsiAccountControllingHost *host) { psiController = host; }

void ImagePlugin::setOptionAccessingHost(OptionAccessingHost *host) { psiOptions = host; }

void ImagePlugin::setStanzaSendingHost(StanzaSendingHost *host) { stanzaSender = host; }

void ImagePlugin::setActiveTabAccessingHost(ActiveTabAccessingHost *host) { activeTab = host; }

void ImagePlugin::actionActivated()
{
    if (!enabled)
        return;

    QString fileName("");
    QString jid       = activeTab->getYourJid();
    QString jidToSend = activeTab->getJid();
    int     account   = 0;
    QString tmpJid("");
    while (jid != (tmpJid = accInfo->getJid(account))) {
        ++account;
        if (tmpJid == "-1")
            return;
    }

    QMenu            m;
    QList<QAction *> list;
    list << new QAction(tr("Open file"), &m) << new QAction(tr("From clipboard"), &m);
    QAction *act = m.exec(list, QCursor::pos());
    if (!act)
        return;

    if ("offline" == accInfo->getStatus(account)) {
        return;
    }

    QPixmap pix;
    QString imageName;
    bool    isClipboard = list.indexOf(act) == 1;
    if (isClipboard) {
        if (!QApplication::clipboard()->mimeData()->hasImage())
            return;

        pix       = QPixmap::fromImage(QApplication::clipboard()->image());
        imageName = QApplication::clipboard()->text();
    } else {
        const QString lastPath = psiOptions->getPluginOption(CONST_LAST_FOLDER, QDir::homePath()).toString();
        fileName               = QFileDialog::getOpenFileName(nullptr, tr("Open Image"), lastPath,
                                                tr("Images (*.png *.gif *.jpg *.jpeg *.ico)"));
        if (fileName.isEmpty())
            return;

        QFile file(fileName);
        if (file.open(QIODevice::ReadOnly)) {
            pix       = QPixmap::fromImage(QImage::fromData(file.readAll()));
            imageName = QFileInfo(file).fileName();

        } else {
            return;
        }
        psiOptions->setPluginOption(CONST_LAST_FOLDER, QFileInfo(file).path());
    }

    QByteArray image;
    QString    mimeType("jpeg");
    if (pix.height() > MAX_SIZE || pix.width() > MAX_SIZE) {
        pix = pix.scaled(MAX_SIZE, MAX_SIZE, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    }
    QBuffer b(&image);
    pix.save(&b, mimeType.toLatin1().constData());
    QString imageBase64(image.toBase64());
    int     length = image.length();
    if (length > 61440) {
        QMessageBox::information(nullptr, tr("The image size is too large."), tr("Image size must be less than 60 kb"));
    }
    QString mType = QLatin1String(sender()->parent()->metaObject()->className()) == "PsiChatDlg" ? "chat" : "groupchat";
    QString body  = tr("Image %1 bytes received.").arg(QString::number(length));
    QString msgHtml = QString("<message type=\"%1\" to=\"%2\" id=\"%3\" >"
                              "<body>%4</body>"
                              "<html xmlns=\"http://jabber.org/protocol/xhtml-im\">"
                              "<body xmlns=\"http://www.w3.org/1999/xhtml\">"
                              "<br/><img src=\"data:image/%5;base64,%6\" alt=\"img\"/> "
                              "</body></html></message>")
                          .arg(mType)
                          .arg(jidToSend)
                          .arg(stanzaSender->uniqueId(account))
                          .arg(body)
                          .arg(mimeType)
                          .arg(imageBase64);

    stanzaSender->sendStanza(account, msgHtml);
    if (isClipboard) {
        psiController->appendSysMsg(account, jidToSend, tr("An image from the clipboard is sent"));
        // TODO for private chat this has to be added as a regular message, otherwise it won't be visible.
    } else {
        psiController->appendSysMsg(account, jidToSend, tr("Image %1 is sent").arg(imageName));
    }
}

QString ImagePlugin::pluginInfo()
{
    return tr("This plugin is designed to send images to roster contacts.\n"
              "Your contact's client must be support XEP-0071: XHTML-IM and support the data:URI scheme.\n"
              "Note: To work correctly, the option options.ui.chat.central-toolbar  must be set to true.");
}

#include "imageplugin.moc"