/
rekall.cpp
343 lines (303 loc) · 13 KB
/
rekall.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
/*
This file is part of Rekall.
Copyright (C) 2013-2015
Project Manager: Clarisse Bardiot
Development & interactive design: Guillaume Jacquemin & Guillaume Marais (http://www.buzzinglight.com)
This file was written by Guillaume Jacquemin.
Rekall is a 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
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 "rekall.h"
Rekall::Rekall(const QStringList &arguments, QWidget *parent) :
QDialog(parent) {
trayIconWorking = false;
trayIconIndex = 0;
trayIconIndexOld = 9999;
Global::rekall = this;
qApp->setAttribute(Qt::AA_UseHighDpiPixmaps);
//Update
updateManager = 0;
forceUpdate = false;
firstTimeOpened = newVersionOfRekall = false;
if(arguments.contains("-forceupdate"))
forceUpdate = true;
//Tray icon
trayTimer.setInterval(500);
connect(&trayTimer, SIGNAL(timeout()), SLOT(trayIconToOnPrivate()));
connect(&trayTimerOff, SIGNAL(timeout()), SLOT(trayIconToOffPrivate()));
QString prefix = "mac";
#ifdef Q_OS_WIN
prefix = "win";
#endif
for(quint16 i = 0 ; i <= 17 ; i++)
trayIcons << QIcon(QString(":/icons/rekall-menubar-%1-%2.png").arg(prefix).arg(i, 2, 10, QChar('0')));
trayIcon = new QSystemTrayIcon(this);
trayIconToOffPrivate();
trayTimer.start();
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), SLOT(trayActivated(QSystemTrayIcon::ActivationReason)));
//Interfaces
Global::userInfos = new UserInfos(this);
Global::http = new Http(this);
Analyse *analyse = new Analyse(this);
Global::analyse = analyse;
connect(analyse, SIGNAL(trayChanged(QString,bool)), SLOT(analyseTrayChanged(QString,bool)));
connect(analyse, SIGNAL(trayIconToOff()), SLOT(trayIconToOff()));
connect(analyse, SIGNAL(trayIconToOn(qint16)), SLOT(trayIconToOn(qint16)));
//Wrapper web
Global::userInfos->setDockIcon(this, false);
Global::webWrapper = new WebWrapper();
/*
VideoPlayer *player = new VideoPlayer();
player->open(QUrl::fromLocalFile("/Users/guillaume/Documents/Rekall/Walden/Captations/captation WALDEN_TPV.mov"));
player->seek(4000);
*/
trayMenu = new QMenu(this);
trayMenu->setSeparatorsCollapsible(true);
trayAnalyse = trayMenu->addAction(tr("File analysis…"));
trayAnalysePause = trayMenu->addAction(tr("Pause analysis"));
trayAnalysePause->setCheckable(true);
connect(trayAnalysePause, SIGNAL(toggled(bool)), SLOT(trayAnalysePaused()));
trayMenu->addSeparator();
trayMenu->addAction(tr("Open welcome page"), this, SLOT(openWebPage()));
trayMenu->addAction(tr("Open welcome page in webrowser"), this, SLOT(openWebPageInBrowser()));
//trayMenu->addAction(tr("Create a new project"), this, SLOT(addProject()));
trayMenu->addSeparator();
trayMenuProjects = trayMenu->addAction(tr("Quit Rekall"), this, SLOT(closeRekall()));
trayIcon->setContextMenu(trayMenu);
trayIcon->show();
trayIconToOnPrivate();
QSettings settings;
quint16 projectCount = settings.beginReadArray("projects");
/*
if(projectCount == 0) {
addProject(new Project("walden", "Walden", true, QFileInfo("/Users/guillaume/Documents/Rekall/Walden"), this));
addProject(new Project("joris-test", "Joris", false, QFileInfo("/Users/guillaume/Documents/Rekall/joris-test"), this));
}
*/
connect(Global::udp, SIGNAL(outgoingMessage(QString,quint16,QString,QList<QVariant>)), SLOT(incomingMessage(QString,quint16,QString,QList<QVariant>)));
for(quint16 projectIndex = 0 ; projectIndex < projectCount ; projectIndex++) {
settings.setArrayIndex(projectIndex);
Project *project = new Project(settings.value("name").toString(), settings.value("friendlyName").toString(), true, QFileInfo(settings.value("path").toString()), this);
addProject(project);
}
settings.endArray();
//foreach(ProjectInterface *project, Global::projects)
// project->load();
//Global settings creation if needed
globalSettings = new QSettings();
if((globalSettings) && ((!globalSettings->childKeys().contains("id")) || (arguments.contains("-newuser")))) {
firstTimeOpened = true;
qsrand(QDateTime::currentDateTime().toTime_t());
updateAnonymousId = QString::number(qrand());
globalSettings->setValue("id", updateAnonymousId);
globalSettings->setValue("version", "");
globalSettings->setValue("updatePeriod", 1);
globalSettings->setValue("lastUpdate", QDateTime(QDate(2000, 01, 01)));
}
//Update management
if((globalSettings) && (globalSettings->childKeys().contains("id"))) {
QDateTime updateLastDate = globalSettings->value("lastUpdate") .toDateTime();
quint16 updatePeriod = globalSettings->value("updatePeriod").toUInt();
updateAnonymousId = globalSettings->value("id") .toString();
QString applicationVersionSettings = globalSettings->value("version").toString();
if(applicationVersionSettings != QCoreApplication::applicationVersion()) {
globalSettings->setValue("version", QCoreApplication::applicationVersion());
firstTimeOpened = true;
}
qDebug("Last update : %s (should update each %d day(s))", qPrintable(updateLastDate.toString("dd/MM/yyyy hh:mm:ss")), updatePeriod);
if((updateLastDate.daysTo(QDateTime::currentDateTime()) >= updatePeriod) || (forceUpdate))
checkForUpdates();
}
askScreenshot = askAddProject = 0;
startTimer(50);
if(firstTimeOpened)
showMessage(tr("Welcome!\nRekall is now running!"));
else
showMessage(tr("Rekall is now running!"));
if(!arguments.contains("-silent"))
openWebPage();
/*
QWebView *webView = new QWebView();
webView->load(QUrl("http://127.0.0.1:23411"));
webView->show();
*/
}
Rekall::~Rekall() {
}
void Rekall::checkForUpdates() {
updateManager = new QNetworkAccessManager(this);
connect(updateManager, SIGNAL(finished(QNetworkReply*)), SLOT(checkForUpdatesFinished(QNetworkReply*)));
QString url = "http://www.rekall.fr/download/updates.php?id=" + updateAnonymousId + "&package=" + (QCoreApplication::applicationName() + "__" + QCoreApplication::applicationVersion()).toLower().replace(" ", "_").replace(".", "_");
qDebug("Checking for updates %s", qPrintable(url));
updateManager->get(QNetworkRequest(QUrl(url, QUrl::TolerantMode)));
}
void Rekall::checkForUpdatesFinished(QNetworkReply *reply) {
if(reply->error() != QNetworkReply::NoError)
qDebug("Network error. %s", qPrintable(reply->errorString()));
else {
if(globalSettings)
globalSettings->setValue("lastUpdate", QDateTime::currentDateTime());
QString info = reply->readAll().trimmed();
if((info.length() > 0) || (forceUpdate))
newVersionOfRekall = true;
//qDebug("Update status : %d", newVersionOfRekall);
}
}
void Rekall::syncSettings() {
QSettings settings;
settings.beginWriteArray("projects");
quint16 projectIndex = 0;
foreach(ProjectInterface *project, Global::projects) {
settings.setArrayIndex(projectIndex++);
settings.setValue("name", project->name);
settings.setValue("friendlyName", project->friendlyName);
settings.setValue("path", project->path.absoluteFilePath());
}
settings.endArray();
settings.sync();
}
void Rekall::addProject() {
QFileInfo dirToOpen(QFileDialog::getExistingDirectory(0, tr("Select a folder where you store the files of your creative project"), Global::pathDocuments.absoluteFilePath()));
if(dirToOpen.exists()) {
addProject(new Project(dirToOpen.baseName(), dirToOpen.baseName(), true, dirToOpen, this));
syncSettings();
showMessage(tr("Rekall is analysing your project %1…").arg(dirToOpen.baseName()));
}
}
void Rekall::addProject(ProjectInterface *project) {
trayMenu->insertAction(trayMenuProjects, project->trayMenuTitle);
trayMenu->insertMenu (trayMenuProjects, project->trayMenuEvents);
trayMenu->insertAction(trayMenuProjects, project->trayMenuFolder);
trayMenu->insertAction(trayMenuProjects, project->trayMenuWeb);
project->trayMenuSeparator = trayMenu->insertSeparator(trayMenuProjects);
Global::projects.append(project);
updateGUI();
project->load();
}
void Rekall::removeProject(ProjectInterface *project) {
trayMenu->removeAction(project->trayMenuEvents->menuAction());
trayMenu->removeAction(project->trayMenuTitle);
trayMenu->removeAction(project->trayMenuFolder);
trayMenu->removeAction(project->trayMenuWeb);
trayMenu->removeAction(project->trayMenuSeparator);
Global::projects.removeOne(project);
project->isRemoved = true;
syncSettings();
updateGUI();
}
void Rekall::updateGUI() {
foreach(ProjectInterface *project, Global::projects)
project->updateGUI();
}
void Rekall::trayIconToOn(qint16 duration) {
if(duration > 0) {
trayIconWorking = true;
trayTimerOff.stop();
trayTimerOff.start(duration);
}
}
void Rekall::trayIconToOff() {
if(trayIconWorking) {
trayTimerOff.stop();
trayTimerOff.start(10);
}
}
void Rekall::analyseTrayChanged(QString text, bool enable) {
if(trayAnalyse->text() != text)
trayAnalyse->setText(text);
if(trayAnalyse->isEnabled() != enable) {
trayAnalyse->setEnabled(enable);
trayAnalysePause->setVisible(enable);
}
}
void Rekall::trayIconToOnPrivate() {
if(trayIconWorking) {
//if(trayIconIndexOld != trayIconIndex)
trayIcon->setIcon(trayIcons.at(trayIconIndex));
trayIconIndexOld = trayIconIndex;
trayIconIndex = (trayIconIndex+1) % (trayIcons.length());
}
}
void Rekall::trayIconToOffPrivate() {
trayIconWorking = false;
trayIconIndex = 4;
//if(trayIconIndexOld != trayIconIndex)
trayIcon->setIcon(trayIcons.at(trayIconIndex));
trayIconIndexOld = trayIconIndex;
}
void Rekall::trayAnalysePaused() {
Global::analyse->paused = trayAnalysePause->isChecked();
}
void Rekall::openWebPage(bool inBrowser) {
QString param;
if(firstTimeOpened)
param += "/pro-intro.html";
Global::webWrapper->openWebPage(QUrl(QString("http://%1:%2%3").arg(Global::http->getLocalHost().ip).arg(Global::http->getPort()).arg(param)), tr("Opening welcome page…"), inBrowser);
}
void Rekall::openWebPageInBrowser() {
openWebPage(true);
}
void Rekall::closeRekall() {
Global::analyse->stop();
syncSettings();
QApplication::exit();
}
void Rekall::incomingMessage(const QString &, quint16, const QString &destination, const QList<QVariant> &valeurs) {
if((destination.startsWith("/rekall/message")) && (valeurs.count()))
showMessage(valeurs.first().toString());
else if(destination.startsWith("/rekall/webpage"))
openWebPage();
}
void Rekall::showMessage(const QString &message) {
trayIcon->showMessage(tr("Rekall"), message);
qDebug("=> %s", qPrintable(message));
}
void Rekall::timerEvent(QTimerEvent *) {
if(askScreenshot == 1) {
askScreenshot = 2;
takeScreenshot();
askScreenshot = 0;
}
if(askAddProject == 1) {
askAddProject = 2;
addProject();
askAddProject = 0;
}
while(askVideoPlayer.count()) {
askVideoPlayer.first().project->videoPlayers->update(askVideoPlayer.first().url, askVideoPlayer.first().askClose, askVideoPlayer.first().title);
askVideoPlayer.removeFirst();
}
}
void Rekall::takeScreenshot() {
showMessage(tr("Snapshot taken!"));
QRectF screenSize = QDesktopWidget().screenGeometry(0);
for(quint16 i = 1 ; i < QDesktopWidget().screenCount() ; i++)
screenSize = screenSize.united(QDesktopWidget().screenGeometry(i));
#ifdef Q_OS_MAC
CGRect screenRect;
screenRect.origin.x = screenSize.x();
screenRect.origin.y = screenSize.y();
screenRect.size.width = screenSize.width();
screenRect.size.height = screenSize.height();
CGImageRef screenshotMac = CGWindowListCreateImage(screenRect, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
//TODO screenshot = QPixmap::fromMacCGImageRef(screenshotMac).toImage();
CGImageRelease(screenshotMac);
#else
screenshot = QPixmap::grabWindow(QApplication::desktop()->winId(), screenSize.x(), screenSize.y(), screenSize.width(), screenSize.height()).toImage();
#endif
}
void Rekall::trayActivated(QSystemTrayIcon::ActivationReason r) {
updateGUI();
#ifdef Q_OS_WIN
if(r == QSystemTrayIcon::Trigger)
openWebPage();
#endif
}