File: conferenceloggerplugin.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 (353 lines) | stat: -rw-r--r-- 10,542 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
 * conferenceloggerplugin.cpp - plugin
 * Copyright (C) 2009-2010  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.
 *
 * 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include <QByteArray>
#include <QComboBox>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QDomElement>

#include "psiplugin.h"
#include "stanzafilter.h"
#include "accountinfoaccessor.h"
#include "accountinfoaccessinghost.h"
#include "applicationinfoaccessor.h"
#include "applicationinfoaccessinghost.h"
#include "optionaccessor.h"
#include "optionaccessinghost.h"
#include "activetabaccessinghost.h"
#include "activetabaccessor.h"
#include "iconfactoryaccessor.h"
#include "iconfactoryaccessinghost.h"
#include "gctoolbariconaccessor.h"
#include "plugininfoprovider.h"

#include "viewer.h"

#define cVer "0.2.2"
#define constHeight "Height"
#define constWidth "Width"
#define constlastItem "lastItem"
#define constShortCut "shortcut"

class ConferenceLogger: public QObject, public PsiPlugin, public StanzaFilter, public AccountInfoAccessor, public ApplicationInfoAccessor, public OptionAccessor,
						public ActiveTabAccessor, public GCToolbarIconAccessor, public IconFactoryAccessor, public PluginInfoProvider
{
	Q_OBJECT
#ifdef HAVE_QT5
	Q_PLUGIN_METADATA(IID "com.psi-plus.ConferenceLogger")
#endif
	Q_INTERFACES(PsiPlugin StanzaFilter AccountInfoAccessor ApplicationInfoAccessor OptionAccessor
				 ActiveTabAccessor GCToolbarIconAccessor IconFactoryAccessor PluginInfoProvider)

	public:
	ConferenceLogger();
	virtual QString name() const;
	virtual QString shortName() const;
	virtual QString version() const;
	virtual QWidget* options();
	virtual bool enable();
	virtual bool disable();
	virtual void applyOptions();
	virtual void restoreOptions(){};
	virtual bool incomingStanza(int account, const QDomElement& xml);
	virtual bool outgoingStanza(int account, QDomElement& xml);
	virtual void setAccountInfoAccessingHost(AccountInfoAccessingHost* host);
	virtual void setApplicationInfoAccessingHost(ApplicationInfoAccessingHost* host);
	virtual void setOptionAccessingHost(OptionAccessingHost* host);
	virtual void optionChanged(const QString& /*option*/){};
	virtual void setActiveTabAccessingHost(ActiveTabAccessingHost* host);
	virtual void setIconFactoryAccessingHost(IconFactoryAccessingHost* host);
	virtual QList < QVariantHash > getGCButtonParam();
	virtual QAction* getGCAction(QObject* , int , const QString& ) { return 0; };
	virtual QString pluginInfo();
	virtual QPixmap icon() const;


private:
	bool enabled;
	AccountInfoAccessingHost *AccInfoHost;
	ApplicationInfoAccessingHost *AppInfoHost;
	OptionAccessingHost *psiOptions;
	ActiveTabAccessingHost* activeTab;
	IconFactoryAccessingHost *IcoHost;
	QString HistoryDir;
	void Logger(QString room, QString from, QString MyJid, QString Text, QString Stamp);
	QComboBox *FilesBox;
	QPushButton *viewButton;
	int Height;
	int Width;
	QString lastItem;
	void showLog(QString filename);

private slots:
	void view();
	void viewFromOpt();
	void onClose(int, int);
};

#ifndef HAVE_QT5
Q_EXPORT_PLUGIN(ConferenceLogger);
#endif

ConferenceLogger::ConferenceLogger() {
	enabled = false;
	AppInfoHost = 0;
	AccInfoHost = 0;
	psiOptions = 0;
	IcoHost = 0;
	activeTab = 0;
	HistoryDir = "";
	FilesBox = 0;
	viewButton = 0;
	Height = 500;
	Width = 600;
	lastItem = "";
}

QString ConferenceLogger::name() const {
	return "Conference Logger Plugin";
}

QString ConferenceLogger::shortName() const {
	return "logger";
}

QString ConferenceLogger::version() const {
	return cVer;
}

bool ConferenceLogger::enable() {
	QFile file(":/conferenceloggerplugin/conferencelogger.png");
	if ( file.open(QIODevice::ReadOnly) ) {
		QByteArray image = file.readAll();
		IcoHost->addIcon("loggerplugin/openlog",image);
		file.close();
	} else {
		enabled = false;
		return enabled;
	}
	if(psiOptions) {
		enabled = true;
		HistoryDir = AppInfoHost->appHistoryDir();
		Height = psiOptions->getPluginOption(constHeight, QVariant(Height)).toInt();
		Width = psiOptions->getPluginOption(constWidth, QVariant(Width)).toInt();
		lastItem = psiOptions->getPluginOption(constlastItem, QVariant(lastItem)).toString();
	}
	return enabled;
}

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

QWidget* ConferenceLogger::options() {
	if(!enabled) {
		return 0;
	}
	QWidget *options = new QWidget();
	QVBoxLayout *layout = new QVBoxLayout(options);
	QLabel *label = new QLabel(tr("You can find your logs here:"));
	QLineEdit *path = new QLineEdit;
	path->setText(HistoryDir);
	path->setEnabled(false);
	FilesBox = new QComboBox();
	QDir dir(HistoryDir);
	foreach(QString file, dir.entryList(QDir::Files)) {
		if(file.contains("_in_")) {
			FilesBox->addItem(file);
		}
	}
	for(int i = FilesBox->count(); i > 0; --i) {
		if(FilesBox->itemText(i) == lastItem) {
			FilesBox->setCurrentIndex(i);
		}
	}
	QHBoxLayout *filesLayout = new QHBoxLayout();
	filesLayout->addWidget(new QLabel(tr("Logs:")));
	filesLayout->addWidget(FilesBox);
	filesLayout->addStretch();
	viewButton = new QPushButton(IcoHost->getIcon("psi/search"), tr("View Log"));
	connect(viewButton, SIGNAL(released()), SLOT(viewFromOpt()));
	QLabel *wikiLink = new QLabel(tr("<a href=\"http://psi-plus.com/wiki/plugins#conference_logger_plugin\">Wiki (Online)</a>"));
	wikiLink->setOpenExternalLinks(true);
	filesLayout->addWidget(viewButton);
	layout->addWidget(label);
	layout->addWidget(path);
	layout->addLayout(filesLayout);
	layout->addStretch();
	layout->addWidget(wikiLink);
	return options;
}

bool ConferenceLogger::incomingStanza(int account, const QDomElement& stanza) {
	if (enabled) {
		if(stanza.tagName() == "message") {
			if(stanza.attribute("type") == "groupchat") {
				QString from = stanza.attribute("from");
				QStringList List = from.split("/");
				QString room = List.takeFirst();
				from = "";
				if(!List.isEmpty()) {
					from = List.join("/");
				}
				QString Stamp = "";
				Stamp = stanza.firstChildElement("x").attribute("stamp");
				QDomElement body = stanza.firstChildElement("body");
				if(!body.isNull()) {
					QString Text = body.text();
					QString MyJid = AccInfoHost->getJid(account);
					MyJid = MyJid.replace("@", "_at_");
					Logger(room, from, MyJid, Text, Stamp);
				}
			}
		}
	}
	return false;
}

bool ConferenceLogger::outgoingStanza(int /*account*/, QDomElement& /*xml*/)
{
	return false;
}

void ConferenceLogger::setAccountInfoAccessingHost(AccountInfoAccessingHost* host) {
	AccInfoHost = host;
}

void ConferenceLogger::setApplicationInfoAccessingHost(ApplicationInfoAccessingHost* host) {
	AppInfoHost = host;
}

void ConferenceLogger::Logger(QString room, QString from, QString MyJid, QString Text, QString Stamp) {
	room = room.replace("@", "_at_");
	room = "_in_" + room;
	if(Stamp == "") {
		Stamp = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
	}
	else {
		Stamp.insert(4, "-");
		Stamp.insert(7, "-");
		Stamp.replace("T", " ");
	}
	QFile file(HistoryDir + QDir::separator() + MyJid + room);
	if(file.open(QIODevice::WriteOnly | QIODevice::Append)) {
		QTextStream out(&file);
		//out.seek(file.size());
		out.setCodec("UTF-8");
		out.setGenerateByteOrderMark(false);
		out << Stamp << "  " << from << ": " << Text << endl;
	}
}

void ConferenceLogger::applyOptions() {
	if (FilesBox == 0)	return;

	QVariant vlastItem(FilesBox->currentText());
	lastItem = vlastItem.toString();
	psiOptions->setPluginOption(constlastItem, vlastItem);
}

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

void ConferenceLogger::viewFromOpt() {
	lastItem = FilesBox->currentText();
	psiOptions->setPluginOption(constlastItem, QVariant(lastItem));
	showLog(lastItem);
}

void ConferenceLogger::view() {
	if(!enabled) return;
	QString Jid = activeTab->getJid();
	QString YourJid = activeTab->getYourJid();
	if(Jid == "" || YourJid == "") {
		return;
	}
	Jid = Jid.replace("@", "_at_");
	QStringList List = YourJid.split("/");
	YourJid = List.takeFirst();
	YourJid = YourJid.replace("@", "_at_");
	QString FName = YourJid + "_in_" + Jid;
	QDir dir(HistoryDir);
	foreach(QString file, dir.entryList(QDir::Files)) {
		if(file == FName) {
			showLog(file);
			break;
		}
	}
}

void ConferenceLogger::showLog(QString filename) {
	filename = HistoryDir + "/" + filename;
	Viewer *v = new Viewer(filename, IcoHost);
	v->resize(Width, Height);
	if(!v->init()) {
		delete(v);
		return;
	}
	connect(v, SIGNAL(onClose(int,int)), this, SLOT(onClose(int,int)));
	v->show();
}

void ConferenceLogger::onClose(int w, int h) {
	Width = w;
	Height = h;
	psiOptions->setPluginOption(constWidth, QVariant(Width));
	psiOptions->setPluginOption(constHeight, QVariant(Height));
}

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

void ConferenceLogger::setIconFactoryAccessingHost(IconFactoryAccessingHost *host) {
	IcoHost = host;
}

QList < QVariantHash > ConferenceLogger::getGCButtonParam() {
	QList< QVariantHash > l;
	QVariantHash hash;
	hash["tooltip"] = QVariant(tr("Groupchat History"));
	hash["icon"] = QVariant(QString("loggerplugin/openlog"));
	hash["reciver"] = qVariantFromValue(qobject_cast<QObject *>(this));
	hash["slot"] = QVariant(SLOT(view()));
	l.push_back(hash);
	return l;
}

QString ConferenceLogger::pluginInfo() {
	return tr("Author: ") +	 "Dealer_WeARE\n"
		 + tr("Email: ") + "wadealer@gmail.com\n\n"
		 + trUtf8("This plugin is designed to save groupchat logs in which the Psi+ user sits.\n"
				  "Groupchats logs can be viewed from the plugin settings or by clicking on the appropriate button on the toolbar in the active window/tab with groupchat.\n\n"
				  "Note: To work correctly, the the Groupchat Toolbar must be enabled.");
}

QPixmap ConferenceLogger::icon() const
{
	return QPixmap(":/conferenceloggerplugin/conferencelogger.png");
}

#include "conferenceloggerplugin.moc"