File: yandexnarod.cpp

package info (click to toggle)
psi-plugins 1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,368 kB
  • sloc: cpp: 42,063; xml: 714; ansic: 84; makefile: 61; sh: 12
file content (250 lines) | stat: -rw-r--r-- 6,351 bytes parent folder | download | duplicates (4)
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
/*
    yandexnarodPlugin

	Copyright (c) 2008-2009 by Alexander Kazarin <boiler@co.ru>
		      2011 Evgeny Khryukin

 ***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************
*/

#include <QFileDialog>

#include "yandexnarod.h"
#include "yandexnarodmanage.h"
#include "authmanager.h"
#include "yandexnarodsettings.h"
#include "uploaddialog.h"
#include "options.h"


yandexnarodPlugin::yandexnarodPlugin()
	: psiOptions(0)
	, psiIcons(0)
	, stanzaSender(0)
	, appInfo(0)
	, popup(0)
	, enabled(false)
	, currentAccount(-1)
	, popupId(0)
{
}

QString yandexnarodPlugin::name() const
{
	return "Yandex Narod Plugin";
}
QString yandexnarodPlugin::shortName() const
{
	return "yandexnarod";
}

QString yandexnarodPlugin::version() const
{
	return VERSION;
}

QWidget* yandexnarodPlugin::options()
{
	if(!enabled) {
		return 0;
	}

	settingswidget = new yandexnarodSettings();
	connect(settingswidget, SIGNAL(testclick()), this,  SLOT(on_btnTest_clicked()));
	connect(settingswidget, SIGNAL(startManager()), this, SLOT(manage_clicked()));

	return settingswidget;
}

bool yandexnarodPlugin::enable()
{
	enabled = true;
	QFile file(":/icons/yandexnarodplugin.png");
	file.open(QIODevice::ReadOnly);
	QByteArray image = file.readAll();
	psiIcons->addIcon("yandexnarod/logo",image);
	file.close();

	Options::instance()->setApplicationInfoAccessingHost(appInfo);
	Options::instance()->setOptionAccessingHost(psiOptions);

	//remove old password option
	QString oldPass = Options::instance()->getOption(CONST_PASS_OLD).toString();
	if(!oldPass.isEmpty()) {
		Options::instance()->setOption(CONST_PASS_OLD, QVariant(""));
		Options::instance()->setOption(CONST_PASS, Options::encodePassword(oldPass));
	}

	popupId = popup->registerOption(name(), 3, "plugins.options." + shortName() + POPUP_OPTION_NAME);

	return enabled;
}

bool yandexnarodPlugin::disable()
{
	enabled = false;
	if(manageDialog)
		delete manageDialog;

	if(uploadwidget) {
		uploadwidget->disconnect();
		delete uploadwidget;
	}

	popup->unregisterOption(name());
	Options::reset();

	return true;
}

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

void yandexnarodPlugin::setIconFactoryAccessingHost(IconFactoryAccessingHost *host)
{
	psiIcons = host;
}

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

void yandexnarodPlugin::setApplicationInfoAccessingHost(ApplicationInfoAccessingHost *host)
{
	appInfo = host;
}

void yandexnarodPlugin::setPopupAccessingHost(PopupAccessingHost *host)
{
	popup = host;
}

void yandexnarodPlugin::applyOptions()
{
	if(settingswidget)
		settingswidget->saveSettings();
}

void yandexnarodPlugin::restoreOptions()
{
	if(settingswidget)
		settingswidget->restoreSettings();
}

QList < QVariantHash > yandexnarodPlugin::getAccountMenuParam()
{
	QList < QVariantHash > list;
	QVariantHash hash;
	hash["icon"] = QVariant(QString("yandexnarod/logo"));
	hash["name"] = QVariant(tr("Open Yandex Narod Manager"));
	hash["reciver"] = qVariantFromValue(qobject_cast<QObject *>(this));
	hash["slot"] = QVariant(SLOT(manage_clicked()));

	list.append(hash);
	return list;
}

QList < QVariantHash > yandexnarodPlugin::getContactMenuParam()
{
	QList < QVariantHash > list;
	QVariantHash hash;
	hash["icon"] = QVariant(QString("yandexnarod/logo"));
	hash["name"] = QVariant(tr("Send file via Yandex Narod"));
	hash["reciver"] = qVariantFromValue(qobject_cast<QObject *>(this));
	hash["slot"] = QVariant(SLOT(actionStart()));

	list.append(hash);
	return list;
}

void yandexnarodPlugin::manage_clicked()
{
	if(!manageDialog) {
		manageDialog = new yandexnarodManage();
		manageDialog->show();
	}
	else {
		manageDialog->raise();
		manageDialog->activateWindow();
	}
}

void yandexnarodPlugin::on_btnTest_clicked()
{
	if(!settingswidget)
		return;

	AuthManager am;
	settingswidget->setStatus(O_M(MAuthStart));
	bool auth = am.go(settingswidget->getLogin(), settingswidget->getPasswd());
	QString rez = auth ? O_M(MAuthOk) : O_M(MAuthError);
	settingswidget->setStatus(rez);
	if(auth) {
		Options::instance()->saveCookies(am.cookies());
	}
}

void yandexnarodPlugin::actionStart()
{
	currentJid = sender()->property("jid").toString();
	currentAccount = sender()->property("account").toInt();
	QString filepath = QFileDialog::getOpenFileName(uploadwidget, O_M(MChooseFile),
							psiOptions->getPluginOption(CONST_LAST_FOLDER).toString());

	if (!filepath.isEmpty()) {
		fi = QFileInfo(filepath);
		psiOptions->setPluginOption(CONST_LAST_FOLDER, fi.dir().path());

		uploadwidget = new uploadDialog();
		connect(uploadwidget, SIGNAL(fileUrl(QString)), this, SLOT(onFileURL(QString)));

		uploadwidget->show();
		uploadwidget->start(filepath);
	}
}

void yandexnarodPlugin::onFileURL(const QString& url)
{
	QString sendmsg = psiOptions->getPluginOption(CONST_TEMPLATE).toString();
	sendmsg.replace("%N", fi.fileName());
	sendmsg.replace("%U", url);
	sendmsg.replace("%S", QString::number(fi.size()));
	uploadwidget->close();

	if(currentAccount != -1 && !currentJid.isEmpty()) {
		stanzaSender->sendMessage(currentAccount, currentJid, stanzaSender->escape(sendmsg), "", "chat");
		showPopup(currentAccount, currentJid, tr("File sent to %1").arg(currentJid));
	}

	currentJid.clear();
	currentAccount = -1;
}

void yandexnarodPlugin::showPopup(int/* account*/, const QString&/* jid*/, const QString& text)
{
	if(popup->popupDuration(name())) {
		popup->initPopup(text, tr("Yandex Narod Plugin"), "yandexnarod/logo", popupId);
	}
}

QString yandexnarodPlugin::pluginInfo()
{
	return trUtf8("Ported from QutIM Yandex.Narod plugin\nhttp://qutim.org/forum/viewtopic.php?f=62&t=711\n\n");
}

QPixmap yandexnarodPlugin::icon() const
{
	return QPixmap(":/icons/yandexnarodplugin.png");
}

Q_EXPORT_PLUGIN(yandexnarodPlugin);