/
openpuush.cpp
526 lines (449 loc) · 14.1 KB
/
openpuush.cpp
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/* OpenPuush, an open-source clone of Puush
* Copyright (C) 2012 Mikhail Slyusarev
*
* 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 3 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 <QPainter>
#include <QPaintEvent>
#include <QPushButton>
#include <QDebug>
#include <QDataStream>
#include <QBuffer>
#include <QDateTime>
#include <QClipboard>
#include <QApplication>
#include <QDesktopWidget>
#include <QFileDialog>
#include <QTimer>
#include <QAction>
#include <QDir>
#include <QTemporaryFile>
#include <QUrl>
#include <QDesktopServices>
#include <phonon/AudioOutput>
#include <phonon/MediaObject>
#include <phonon/MediaSource>
#include <phonon/VideoWidget>
#include <QxtGlobalShortcut>
#include "dropbox.hpp"
#include "openpuush.hpp"
#include "screenshot_overlay.hpp"
#include "config.hpp"
#include "configwindow.hpp"
#include "tray_menu.hpp"
#define set_shortcut(s,e) s->setShortcut(QKeySequence(config::load(config::e).toString()))
static const QString NOT_AUTHORIZED_MESSAGE = "Your action could not be completed because\n"
"OpenPuush is not linked with a Dropbox account.";
openpuush::openpuush(QObject *parent) :
QObject(parent),
base_path(QDir::currentPath()),
dropbox_authenticated(false),
shortcuts_enabled(true),
follow_tray_icon_link(false)
{
QApplication::setQuitOnLastWindowClosed(false);
if (config::first_run())
{
config::defaults();
}
load_start_on_boot();
// order is important here!
init_dropbox();
init_ss_overlay();
init_file_dialog();
init_shortcuts();
init_conf_win();
init_tray_icon();
init_tray_icon_context_menu();
}
openpuush::~openpuush()
{
delete db;
delete conf_win;
delete ss_overlay;
delete menu;
delete tray_icon;
delete file_dialog;
delete info_update_timer;
delete fullscreen_shortcut;
delete current_window_shortcut;
delete ss_overlay_shortcut;
delete file_shortcut;
delete upload_clipboard_shortcut;
delete toggle_functionality_shortcut;
}
void openpuush::init_conf_win()
{
conf_win = new configwindow();
connect(conf_win, SIGNAL(set_shortcuts_enabled(bool,bool)),
this, SLOT(set_shortcuts_enabled(bool,bool)));
connect(conf_win, SIGNAL(error_occurred(QString)), this, SLOT(error_occurred(QString)));
connect(conf_win, SIGNAL(toggle_start_on_boot()), this, SLOT(load_start_on_boot()));
connect(conf_win, SIGNAL(login_pressed()), this, SLOT(db_login()));
connect(conf_win, SIGNAL(logout_pressed()), this, SLOT(db_logout()));
}
void openpuush::init_ss_overlay()
{
ss_overlay = new screenshot_overlay();
connect(ss_overlay, SIGNAL(got_screenshot(QPixmap)), this, SLOT(got_screenshot(QPixmap)));
}
void openpuush::init_file_dialog()
{
file_dialog = new QFileDialog();
file_dialog->setFileMode(QFileDialog::AnyFile);
connect(file_dialog, SIGNAL(fileSelected(QString)), this, SLOT(upload_file(QString)));
}
void openpuush::init_dropbox()
{
db = new dropbox();
connect(db, SIGNAL(got_authorized()), this, SLOT(db_authorized()));
connect(db, SIGNAL(upload_progress(qint64, qint64)),
this, SLOT(db_upload_progress(qint64,qint64)));
connect(db, SIGNAL(upload_finished()), this, SLOT(db_upload_finished()));
connect(db, SIGNAL(link_ready(QString)), this, SLOT(db_link_ready(QString)));
connect(db, SIGNAL(got_account_info(QMap<QString,QString>)),
this, SLOT(db_info_received(QMap<QString,QString>)));
info_update_timer = new QTimer();
connect(info_update_timer, SIGNAL(timeout()), this, SLOT(update_db_info()));
QTimer::singleShot(0, db, SLOT(check_authentication()));
}
void openpuush::init_tray_icon()
{
tray_icon = new QSystemTrayIcon(this);
tray_icon->setIcon(QIcon(base_path + "/icons/0.png"));
tray_icon->show();
connect(tray_icon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(tray_icon_activated(QSystemTrayIcon::ActivationReason)));
connect(tray_icon, SIGNAL(messageClicked()), this, SLOT(follow_link()));
}
void openpuush::init_shortcuts()
{
fullscreen_shortcut = new QxtGlobalShortcut(this);
connect(fullscreen_shortcut, SIGNAL(activated()), this, SLOT(fullscreen_ss()));
current_window_shortcut = new QxtGlobalShortcut(this);
connect(current_window_shortcut, SIGNAL(activated()), this, SLOT(upload_current_window()));
ss_overlay_shortcut = new QxtGlobalShortcut(this);
connect(ss_overlay_shortcut, SIGNAL(activated()), this, SLOT(show_ss_overlay()));
file_shortcut = new QxtGlobalShortcut(this);
connect(file_shortcut, SIGNAL(activated()), file_dialog, SLOT(show()));
upload_clipboard_shortcut = new QxtGlobalShortcut(this);
connect(upload_clipboard_shortcut, SIGNAL(activated()), this, SLOT(upload_clipboard()));
toggle_functionality_shortcut = new QxtGlobalShortcut(this);
connect(toggle_functionality_shortcut, SIGNAL(activated()), this, SLOT(toggle_functionality()));
update_shortcuts();
}
void openpuush::init_tray_icon_context_menu()
{
menu = new tray_menu();
}
void openpuush::update_shortcuts()
{
set_shortcut(fullscreen_shortcut, CAPTURE_FULLSCREEN);
set_shortcut(current_window_shortcut, CAPTURE_CURRENT_WINDOW);
set_shortcut(ss_overlay_shortcut, CAPTURE_AREA);
set_shortcut(file_shortcut, UPLOAD_FILE);
set_shortcut(upload_clipboard_shortcut, UPLOAD_CLIPBOARD);
set_shortcut(toggle_functionality_shortcut, TOGGLE_FUNCTIONALITY);
}
void openpuush::update_db_info()
{
QTimer::singleShot(0, db, SLOT(account_info()));
}
void openpuush::db_authorized()
{
dropbox_authenticated = true;
QTimer::singleShot(200, db, SLOT(account_info()));
info_update_timer->start(1000 * 60 * 5);
conf_win->set_logged_in(configwindow::LOGGED_IN);
}
void openpuush::db_upload_progress(qint64 sent, qint64 total)
{
int img_num = (sent * 10) / total;
tray_icon->setIcon(QIcon(base_path + QString("/icons/%1.png").arg(img_num)));
qDebug() << ((sent * 100) / total) << "%";
}
void openpuush::db_upload_finished()
{
tray_icon->setIcon(QIcon(base_path + "/icons/success.png"));
QTimer::singleShot(2000, this, SLOT(tray_icon_normal()));
tray_icon->showMessage("Success", link, QSystemTrayIcon::NoIcon, 2000);
follow_tray_icon_link = true;
if (config::load(config::SOUND_NOTIFICATION).toBool())
{
/*Phonon::createPlayer(Phonon::VideoCategory,
Phonon::MediaSource(base_path + "/sounds/fin.ogg"))->play();*/
}
if (config::load(config::COPY_LINK).toBool())
{
QApplication::clipboard()->setText(link);
}
if (config::load(config::OPEN_LINK).toBool())
{
QDesktopServices::openUrl(link);
}
}
void openpuush::db_upload_error()
{
tray_icon->setIcon(QIcon(base_path + "/icons/error.png"));
QTimer::singleShot(2000, this, SLOT(tray_icon_normal()));
tray_icon->showMessage("Error", "The current action failed.");
/*Phonon::createPlayer(Phonon::VideoCategory,
Phonon::MediaSource(base_path + "/sounds/err.ogg"))->play();*/
}
void openpuush::db_link_ready(QString link)
{
this->link = link;
qDebug() << link;
}
void openpuush::db_info_received(QMap<QString, QString> info)
{
conf_win->update_account_info(info.value("display_name"), info.value("used"));
}
void openpuush::db_login()
{
db->check_authentication();
}
void openpuush::db_logout()
{
info_update_timer->stop();
dropbox_authenticated = false;
QTimer::singleShot(0, db, SLOT(logout()));
conf_win->set_logged_in(configwindow::LOGGED_OUT);
}
void openpuush::got_screenshot(QPixmap ss)
{
ss_overlay->close();
bool saved_as_jpeg = false;
QByteArray bytes;
QBuffer buf(&bytes);
buf.open(QIODevice::ReadWrite);
ss.save(&buf, "png");
if (config::load(config::SMART_COMPRESSION).toBool())
{
QByteArray jpeg_bytes;
QBuffer jpeg_buf(&jpeg_bytes);
jpeg_buf.open(QIODevice::ReadWrite);
ss.save(&jpeg_buf, "jpeg");
if (bytes.length() < jpeg_bytes.length())
{
saved_as_jpeg = true;
bytes.clear();
bytes = jpeg_bytes;
}
}
QString name = QString("Screenshot-%1.").arg(QDateTime::currentDateTime().toTime_t());
name += saved_as_jpeg ? "jpeg" : "png";
if (bytes.length() > 0)
{
if (config::load(config::SAVE_COPY).toBool())
{
ss.save(config::load(config::LOCAL_COPY_PATH).toString() + "/" + name, "png");
}
db->file_put(bytes, name);
}
else
{
db_upload_error();
}
}
void openpuush::show_ss_overlay()
{
if (dropbox_authenticated)
{
ss_overlay->show();
}
else
{
error_occurred(NOT_AUTHORIZED_MESSAGE);
}
}
void openpuush::fullscreen_ss()
{
if (dropbox_authenticated)
{
QRect f = ss_overlay->geometry();
got_screenshot(QPixmap::grabWindow(QApplication::desktop()->winId(),
f.left(), f.top(), f.width(), f.height()));
}
else
{
error_occurred(NOT_AUTHORIZED_MESSAGE);
}
}
void openpuush::upload_file(QString filename)
{
if (dropbox_authenticated)
{
// TODO: at some point revisit this and make it not load
// the entire file into memory before upload
QFile file(filename);
QString name = QFileInfo(file).fileName();
if (file.open(QIODevice::ReadOnly))
{
QByteArray contents = file.readAll();
db->file_put(contents, name);
}
else
{
db_upload_error();
}
}
else
{
error_occurred(NOT_AUTHORIZED_MESSAGE);
}
}
void openpuush::upload_current_window()
{
// TODO: implement
error_occurred("The feature \"upload current window\" is not\nyet implemented.");
}
void openpuush::upload_clipboard()
{
if (dropbox_authenticated)
{
QString text = QApplication::clipboard()->text().trimmed();
QByteArray contents(text.toAscii());
QUrl url(text);
if (url.isValid() && url.isLocalFile())
{
upload_file(url.toLocalFile());
}
else
{
QString name = QString("Text-%1.txt").arg(QDateTime::currentDateTime().toTime_t());
db->file_put(contents, name);
}
}
else
{
error_occurred(NOT_AUTHORIZED_MESSAGE);
}
}
void openpuush::toggle_functionality()
{
set_shortcuts_enabled((shortcuts_enabled = !shortcuts_enabled), true);
tray_icon->showMessage("Status", QString("OpenPuush is now %1.")
.arg(shortcuts_enabled ? "enabled" : "disabled"),
QSystemTrayIcon::Information, 2000);
}
void openpuush::tray_icon_activated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason)
{
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick:
if (config::load(config::SHOW_SETTINGS).toBool())
{
if (conf_win->isHidden())
{
conf_win->show();
}
else
{
conf_win->hide();
}
}
else if (config::load(config::BEGIN_SCREEN_CAPTURE).toBool())
{
show_ss_overlay();
}
else if (config::load(config::OPEN_FILE_DIALOG).toBool())
{
file_dialog->show();
}
break;
case QSystemTrayIcon::Context:
if (menu->isHidden())
{
menu->show();
}
else
{
menu->hide();
}
break;
default:
break;
}
}
void openpuush::follow_link()
{
if (follow_tray_icon_link)
{
QDesktopServices::openUrl(link);
follow_tray_icon_link = false;
}
}
void openpuush::tray_icon_normal()
{
tray_icon->setIcon(QIcon(base_path + "/icons/0.png"));
}
void openpuush::set_shortcuts_enabled(bool enabled, bool all_but_toggle_functionality)
{
if (enabled)
{
update_shortcuts();
}
fullscreen_shortcut->setEnabled(enabled);
current_window_shortcut->setEnabled(enabled);
ss_overlay_shortcut->setEnabled(enabled);
file_shortcut->setEnabled(enabled);
upload_clipboard_shortcut->setEnabled(enabled);
if (!all_but_toggle_functionality)
{
toggle_functionality_shortcut->setEnabled(enabled);
}
}
void openpuush::error_occurred(QString description)
{
tray_icon->showMessage("Error", description, QSystemTrayIcon::Critical, 2000);
}
void openpuush::load_start_on_boot()
{
// TODO: implement for windows and osx
if (config::load(config::START_ON_BOOT).toBool())
{
#if defined(Q_OS_LINUX)
QFile xsession(QDir::homePath() + "/" + ".xsession");
bool existed = xsession.exists();
xsession.open(QIODevice::Append | QIODevice::Text);
QTextStream out(&xsession);
if (!existed)
{
out << "#!/bin/sh\r\n";
}
out << "./.openpuu.sh\r\n";
QFile opscript(QDir::homePath() + "/" + ".openpuu.sh");
opscript.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream o(&opscript);
o << QApplication::applicationFilePath() + " &\r\n";
#elif defined(Q_OS_MAC)
#elif defined(Q_OS_WIN32)
#else
#warning "Compiling for a system without support for starting OpenPuush on boot."
error_occurred("Starting on boot is not yet supported on this platform");
#endif
}
else
{
#if defined(Q_OS_LINUX)
QDir::home().remove(".openpuu.sh");
#elif defined(Q_OS_MAC)
#elif defined(Q_OS_WIN32)
#else
error_occurred("Starting on boot is not yet supported on this platform");
#endif
}
}