File: viewmaildlg.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 (165 lines) | stat: -rw-r--r-- 4,047 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
/*
 * viewmaildlg.cpp - plugin
 * Copyright (C) 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.
 *
 * 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 "viewmaildlg.h"
#include <QDesktopServices>
#include <QWheelEvent>

static const QString mailBoxUrl = "https://mail.google.com/mail/#inbox";

ViewMailDlg::ViewMailDlg(QList<MailItem> l, IconFactoryAccessingHost* host, QWidget *p)
	: QDialog(p, Qt::Window)
	, items_(l)
	, currentItem_(-1)
{
	setAttribute(Qt::WA_DeleteOnClose);
	ui_.setupUi(this);
	ui_.tb_next->setIcon(host->getIcon("psi/arrowRight"));
	ui_.tb_prev->setIcon(host->getIcon("psi/arrowLeft"));
	ui_.pb_close->setIcon(style()->standardIcon(QStyle::SP_DialogCloseButton));
	ui_.pb_browse->setIcon(style()->standardIcon(QStyle::SP_BrowserReload));

	connect(ui_.tb_next, SIGNAL(clicked()), SLOT(showNext()));
	connect(ui_.tb_prev, SIGNAL(clicked()), SLOT(showPrev()));
	connect(ui_.pb_browse, SIGNAL(clicked()), SLOT(browse()));
	connect(ui_.te_text, SIGNAL(anchorClicked(QUrl)), SLOT(anchorClicked(QUrl)));

	if(!items_.isEmpty()) {
		currentItem_ = 0;
		showItem(currentItem_);
	}

	updateCaption();
}

void ViewMailDlg::updateCaption()
{
	setWindowTitle( caption() );
}

QString ViewMailDlg::caption() const
{
	return tr("[%1/%2] E-Mail")
			.arg(QString::number(currentItem_+1))
			.arg(items_.size());
}

void ViewMailDlg::appendItems(QList<MailItem> l)
{
	items_.append(l);
	if(currentItem_ == -1) {
		currentItem_ = 0;
		showItem(currentItem_);
	}
	updateButtons();
	updateCaption();
}

void ViewMailDlg::updateButtons()
{
	if(items_.isEmpty()) {
		ui_.tb_next->setEnabled(false);
		ui_.tb_prev->setEnabled(false);
	}
	else {
		ui_.tb_prev->setEnabled(currentItem_ != 0);
		ui_.tb_next->setEnabled(items_.size()-1 > currentItem_);
	}
}

void ViewMailDlg::showNext()
{
	if(ui_.tb_next->isEnabled())
		showItem(++currentItem_);
}

void ViewMailDlg::showPrev()
{
	if(ui_.tb_prev->isEnabled())
		showItem(--currentItem_);
}

void ViewMailDlg::showItem(int num)
{
	ui_.le_account->clear();
	ui_.le_from->clear();
	ui_.le_subject->clear();
	ui_.te_text->clear();

	if(num != -1
	   && !items_.isEmpty()
	   && num < items_.size() )
	{
		MailItem me = items_.at(num);
		ui_.le_account->setText(me.account);
		ui_.le_account->setCursorPosition(0);
		ui_.le_from->setText(me.from);
		ui_.le_from->setCursorPosition(0);
		ui_.le_subject->setText(me.subject);
		ui_.le_subject->setCursorPosition(0);
		//FIXMI gmail отдает какой-то непонятный урл
		QRegExp re("th=([0-9]+)&");
		QString text = me.text;
		if(me.url.contains(re)) {
			QString url = mailBoxUrl + "/";
			QString tmp = re.cap(1);
			url += QString::number(tmp.toLongLong(), 16);
			text += QString("<br><br><a href=\"%1\">%2</a>").arg(url, tr("Open in browser"));
		}
		ui_.te_text->setHtml(text);
	}
	updateButtons();
	updateCaption();
}

void ViewMailDlg::browse()
{
	QDesktopServices::openUrl(QUrl(mailBoxUrl));
}

void ViewMailDlg::anchorClicked(const QUrl &url)
{
	if(!url.isEmpty()) {
		QDesktopServices::openUrl(url);
		if(items_.count() < 2) {
			close();
		}
	}
}

void ViewMailDlg::wheelEvent(QWheelEvent *e)
{
	if(e->delta() < 0) {
		showNext();
	}
	else {
		showPrev();
	}
	e->accept();
}

QString ViewMailDlg::mailItemToText(const MailItem &mi)
{
	QStringList lst = QStringList() << mi.from
			  << mi.subject << mi.text;
	QString text = lst.join("\n") + "\n";

	return text;
}