-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathguibehind.cpp
660 lines (567 loc) · 20.2 KB
/
guibehind.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/* DUKTO - A simple, fast and multi-platform file transfer tool for LAN users
* Copyright (C) 2011 Emanuele Colombo
* 2020 KylinSoft Co., Ltd.
*
* 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 program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "guibehind.h"
#include "settings.h"
#include "miniwebserver.h"
#include "duktowindow.h"
#include "platform.h"
#include <QQuickView>
#include <QQmlContext>
#include <QQmlProperty>
#include <QTimer>
#include <QDesktopServices>
#include <QDir>
#include <QFileDialog>
#include <QClipboard>
#include <QApplication>
#include <QGraphicsObject>
#include <QRegExp>
#include <QThread>
#include <QTemporaryFile>
#include <QDesktopWidget>
#include <QScreen>
#include <QTime>
GuiBehind::GuiBehind(DuktoWindow* view) :
QObject(NULL), mView(view), mPeriodicHelloTimer(NULL),
mMiniWebServer(NULL), mSettings(NULL)
{
// 通信管理中心
mDuktoProtocol = new DuktoProtocol(this);
// 头像web服务器
mMiniWebServer = new MiniWebServer();
// 设置中心
mSettings = new Settings(this);
// 添加 "本机" 到好友列表strip
mBuddiesList.addMeElement(tr(" (You)"));
// 添加 "使用IP地址" 到好友列表
mBuddiesList.addIpElement(tr("Use IP Address"), tr("Contact with parent network"));
// 响应用户手册
mDaemonIpcDbus = new DaemonIpcDbus();
mView->setGuiBehindReference(this);
QDir::setCurrent(mSettings->currentPath());
mTheme.setThemeColor(mSettings->themeColor());
// qml属性填充
view->rootContext()->setContextProperty("buddiesListData", &mBuddiesList);
view->rootContext()->setContextProperty("recentListData", &mRecentList);
view->rootContext()->setContextProperty("ipAddressesData", &mIpAddresses);
view->rootContext()->setContextProperty("guiBehind", this);
view->rootContext()->setContextProperty("theme", &mTheme);
connect(mDuktoProtocol, SIGNAL(peerListAdded(Peer)), this, SLOT(peerListAdded(Peer)));
connect(mDuktoProtocol, SIGNAL(peerListRemoved(Peer)), this, SLOT(peerListRemoved(Peer)));
// 上线发广播
/*init udp server and tcp server*/
mDuktoProtocol->initialize();
mDuktoProtocol->sayHello(QHostAddress::Broadcast);
// 定时发广播
mPeriodicHelloTimer = new QTimer(this);
connect(mPeriodicHelloTimer, SIGNAL(timeout()), this, SLOT(periodicHello()));
mPeriodicHelloTimer->start(60000);
// 新消息闪烁提示相关
this->alertTimer = new QTimer(this);
this->alertTimer->setInterval(300);
this->connect(this->alertTimer, SIGNAL(timeout()), this, SLOT(msgAlert()));
this->iconDukto = QIcon::fromTheme("kylin-ipmsg");
QString themeName = this->iconDukto.themeName();
QString iconPath = mSettings->iconPath();
QFileInfo fi(iconPath);
// icon图标路径
iconPath = "/usr/share/icons/"+ themeName + "/64x64/apps/kylin-ipmsg.png";
fi.setFile(iconPath);
if (fi.exists()) {
mSettings->saveIconPath(iconPath);
}
else {
// 保险措施
iconPath = "/usr/share/pixmaps/kylin-ipmsg.png";
mSettings->saveIconPath(iconPath);
}
// qml访问本地图片需使用绝对路径
this->setIconPath(iconPath);
// 加载 GUI
view->setSource(QUrl("qrc:/qml/dukto/Dukto.qml"));
//TODO: check QtQuick 2 restoreGeometry
// view->restoreGeometry(mSettings->windowGeometry());
// view->setGeometry(mSettings->windowGeometry());
this->iconBlank = QIcon(":/qml/dukto/Blank.png");
this->cwx = -1;
this->cwy = -1;
this->timeFlag = 0;
}
GuiBehind::~GuiBehind(){
qDebug() << "GuiBehind::~GuiBehind";
mDuktoProtocol->sayGoodbye();
if (mMiniWebServer) mMiniWebServer->deleteLater();
if (mPeriodicHelloTimer) mPeriodicHelloTimer->deleteLater();
if (mView) mView->deleteLater();
if (mSettings) mSettings->deleteLater();
}
// 在列表中添加新好友
void GuiBehind::peerListAdded(Peer peer){
mBuddiesList.addBuddy(peer);
}
// 收到goodbye消息,在列表中删除对应好友
void GuiBehind::peerListRemoved(Peer peer){
mBuddiesList.removeBuddy(peer.mac);
}
/*send text finish add receive list*/
void GuiBehind::sendTextComplete_add_recentlist(QString text , QString mac)
{
QStandardItem *buddy = NULL;
buddy = mBuddiesList.buddyByMac(mac);
qDebug() << "sendTextComplete_add_recentlist fun call args ->" << "mac : " << mac << "text : " << text << "buddy list point : " << buddy;
QString userName;
QString ip_mac;
userName.clear();
ip_mac.clear();
if (buddy != NULL) {
userName = buddy->data(BuddyListItemModel::Username).toString();
ip_mac = buddy->data(BuddyListItemModel::Ip).toString();
qDebug() << "username :" << userName << "ip_mac :" << ip_mac;
/*delete recent item and again add*/
this->removeRecentItem(mac);
mRecentList.addRecent("Text snippet", text, "text", userName , 0 , ip_mac);
}
qDebug() << "text: " << text;
// showSendPage(mac);
// QString tmpIp = ip_mac.split(" ")[0];
// if (this->cws.contains(tmpIp)) {
// // qDebug() << "chat log: " << this->cws.value(tmpIp)->getTextLog();
// this->cws.value(tmpIp)->close();
// }
// this->cws.value(mac)->addTextLog(text);
return;
}
/*send file finish add receive list*/
void GuiBehind::sendFileComplete_add_recentlist(QStringList *files, qint64 totalSize, QString dir, QString mac)
{
QStandardItem *buddy = NULL;
buddy = mBuddiesList.buddyByMac(mac);
qDebug() << "sendFileComplete_add_recentlist fun call args ->" << "mac : " << mac << "dir : " << dir << "buddy list point : " << buddy;
QString userName;
QString ip_mac;
userName.clear();
ip_mac.clear();
if (buddy != NULL) {
userName = buddy->data(BuddyListItemModel::Username).toString();
ip_mac = buddy->data(BuddyListItemModel::Ip).toString();
qDebug() << "username :" << userName << "ip_mac :" << ip_mac;
/*delete recent item and again add*/
this->removeRecentItem(mac);
// 添加到最近聊天列表
QDir d(".");
QString ip_mac = buddy->data(BuddyListItemModel::Ip).toString();
if (files->size() == 1)
mRecentList.addRecent(files->at(0), d.absoluteFilePath(files->at(0)), "file", userName, totalSize, ip_mac);
else
mRecentList.addRecent("Files and folders", d.absolutePath(), "misc", userName, totalSize, ip_mac);
delete files;
files = NULL;
}
return;
}
// 一次文件接收完成
/*
* Parameters:
* files: receive files
* totalsize: total size
* dir: dir
* mac: mac address
* Return :
*/
void GuiBehind::receiveFileComplete(QStringList *files, qint64 totalSize, QString dir, QString mac){
QDir d(".");
QStandardItem *buddy = mBuddiesList.buddyByMac(mac);
QString userName = buddy->data(BuddyListItemModel::Username).toString();
// 时间头
QDateTime time = QDateTime::currentDateTime();
QString tHead = "<font color=" + Theme::DEFAULT_THEME_COLOR + ">";
tHead += userName;
tHead += " (";
tHead += time.toString("hh:mm:ss");
tHead += ")</font>";
QString msg = "";
QStringList tmpList;
// 收到的是文件
if (dir == ""){
// msg = "<font color=" + mTheme.color4() + ">收到文件 ";
msg = "<font color=" + mTheme.color4() + ">"+tr("Received file" );
if(files->size() == 1){
msg += "'";
msg += files->at(0);
// msg += "' 大小 (";
msg += "' "+tr("size")+" (";
msg += this->sizeHuman(totalSize);
msg += ")</font><br/>";
// msg += "<a href='" + d.absoluteFilePath(files->at(0)) + "'>打开</a>";
msg += "<a href='" + d.absoluteFilePath(files->at(0)) + "'>"+tr("Open")+"</a>";
// msg += " <a href='" + d.absolutePath() + "'>打开目录</a>";
msg += " <a href='" + d.absolutePath() + "'>"+tr("Open directory")+"</a>";
}else{
msg += QString::number(files->size());
// msg += " 个";
msg += " "+tr("files");
// msg += " 共计大小 (";
msg += " "+tr("Total size")+" (";
msg += this->sizeHuman(totalSize);
msg += ")<br/>";
for(int i = 0; i < files->size(); i ++){
msg += "'";
msg += files->at(i);
msg += "' ";
}
msg += "</font><br/><a href='" + d.absolutePath() + "'>"+tr("Open directory")+"</a>";
}
// 收到的是文件夹
}else{
// msg = "<font color=" + mTheme.color4() + ">收到文件夹 '" + dir + "' 共计大小 (";
msg = "<font color=" + mTheme.color4() + ">"+tr("Received folder")+" '" + dir + "' "+tr("Total size")+" (";
msg += this->sizeHuman(totalSize);
msg += ")</font><br/>";
msg += "<a href='" + d.absolutePath() + "'>"+tr("Open directory")+"</a>";
}
// 已有该对话框,直接append
if(this->cws.contains(mac)){
ChatWidget *cw = this->cws.value(mac);
cw->addLogDirect(tHead);
cw->addLogDirect(msg);
cw->stopTransfer(false);
// 没有该对话框,先保存在tmpMsgs中
}else{
// 已有该tmpMsg,取出继续使用
if(this->tmpMsgs.contains(mac)){
tmpList = this->tmpMsgs.value(mac);
}
tmpList.append(tHead);
tmpList.append(msg);
this->tmpMsgs.insert(mac, tmpList);
}
// 将同名的历史聊天对象删除,再执行下面的addRecent,确保每一个对象只在列表中出现一个
this->removeRecentItem(mac);
// 添加到最近聊天列表
QString ip_mac = buddy->data(BuddyListItemModel::Ip).toString();
if (files->size() == 1)
mRecentList.addRecent(files->at(0), d.absoluteFilePath(files->at(0)), "file", userName, totalSize, ip_mac);
else
mRecentList.addRecent("Files and folders", d.absolutePath(), "misc", userName, totalSize, ip_mac);
delete files;
files = NULL;
// 界面新消息提醒
newMsgAlert(mac);
}
// 一次文字接收完成
/*
* Parameters:
* text: text msg
* mac: mac address
* Return :
*/
void GuiBehind::receiveTextComplete(QString text, QString mac){
qDebug() << "mac : " << mac << "text" << text ;
QStandardItem *buddy = NULL;
buddy = mBuddiesList.buddyByMac(mac);
qDebug() << "buddy" << buddy;
QString userName;
QString ip_mac;
if(buddy != NULL)
{
userName = buddy->data(BuddyListItemModel::Username).toString();
ip_mac = buddy->data(BuddyListItemModel::Ip).toString();
qDebug() << "username :" << userName << "ip_mac :" << ip_mac;
}
// 已有该对话框,直接append
if(this->cws.contains(mac)){
ChatWidget *cw = this->cws.value(mac);
cw->addTextLog(text);
// 没有该对话框,先保存在tmpMsgs中
}else{
QDateTime time = QDateTime::currentDateTime();
QString tHead= "<font color=" + Theme::DEFAULT_THEME_COLOR + ">";
tHead += userName;
tHead += " (";
tHead += time.toString("hh:mm:ss");
tHead += ")</font>";
if(this->tmpMsgs.contains(mac)){
QStringList tmpList = this->tmpMsgs.value(mac);
tmpList.append(tHead);
tmpList.append(text);
this->tmpMsgs.insert(mac, tmpList);
}else{
QStringList tmpList;
tmpList.append(tHead);
tmpList.append(text);
this->tmpMsgs.insert(mac, tmpList);
}
}
// 将同名的历史聊天对象删除,再执行下面的addRecent,确保每一个对象只在列表中出现一个
this->removeRecentItem(mac);
// 添加到最近聊天列表
mRecentList.addRecent("Text snippet", text, "text", userName, 0, ip_mac);
// 界面新消息提醒
newMsgAlert(mac);
}
// 删除最近聊天列表中的指定条目
/*
* Parameters:
* mac: mac address
* Return :
*/
void GuiBehind::removeRecentItem(QString mac){
for (int i = 0; i < mRecentList.rowCount(); i ++) {
QStandardItem *oneItem = mRecentList.item(0);
QVariant sender = oneItem->data(RecentListItemModel::Mac);
QStringList mac_list = sender.toString().split(" ");
if (mac_list.count() >= 2) {
QString mac_ = mac_list.at(1);
if(mac == mac_){
mRecentList.removeRow(i);
break;
}
}
}
}
// 显示聊天对话窗口
/*
* Parameters:
* ip_mac: ip and mac address string
* Return :
*/
void GuiBehind::showSendPage(QString ip_mac)
{
qDebug()<<"打开聊天窗口:"<<ip_mac;
if (ip_mac == "") {
return ;
}
QStringList sections = ip_mac.split(" ");
QString mac = "";
if(sections.size() == 1){
mac = ip_mac;
}else{
mac = sections[1];
}
QStandardItem *buddy = mBuddiesList.buddyByMac(mac);
if (buddy == NULL) return;
// 已经有实例
if(this->cws.contains(mac)){
foreach (ChatWidget *cw_, this->cws){
cw_->focusOut();
}
ChatWidget *cw = this->cws.value(mac);
cw->showme();
cw->show();
cw->raise();
cw->focusIn();
// 使用wmctrl命令切换聊天窗口
// QString wmcmd = "wmctrl -a " + cw->windowTitle();
// system(wmcmd.toUtf8().data());
// 创建新实例
}else{
ChatWidget *cw = createCW(mac);
cw->showme();
cw->show();
}
// 停止头像动画和任务栏动画
buddy->setData(false, BuddyListItemModel::ShowMsgAnim);
this->alertTimer->stop();
// this->mView->setWindowIcon(this->iconDukto);
}
// 创建新的对话框实例
/*
* Parameters:
* mac: mac address
* Return :
*/
ChatWidget * GuiBehind::createCW(QString mac){
QStandardItem *buddy = mBuddiesList.buddyByMac(mac);
ChatWidget *cw = new ChatWidget();
cw->theme = &mTheme;
cw->buddies = &(mBuddiesList.mItemsMap);
cw->dbuddy->fillFromItem(buddy);
mIpAddresses.refreshIpList();
cw->myIp = mIpAddresses.myIp;
cw->firstStyle();
connect(cw, SIGNAL(reSaveCw(QString)), this, SLOT(reSaveCw(QString)));
connect(cw, SIGNAL(reBindSocket(ChatWidget*)), mDuktoProtocol, SLOT(connectSocketAndChatWidget(ChatWidget*)));
this->cws.insert(QString(mac), cw);
// 有tmp消息,全部append到对话框中
if(this->tmpMsgs.contains(mac)){
QStringList tmpList = this->tmpMsgs.value(mac);
for(int i = 0; i < tmpList.length(); i ++){
QString msg = tmpList[i];
cw->addLogDirect(msg);
}
this->tmpMsgs.remove(mac);
}
// 多对话框初始位置偏移
if(this->cwx == -1){
QDesktopWidget* desktop = QApplication::desktop();
this->cwx = (desktop->width() - cw->width()) / 2;
this->cwy = (desktop->height() - cw->height()) / 2;
}else{
this->cwx += 25;
this->cwy += 25;
}
cw->move(this->cwx, this->cwy);
return cw;
}
// 使用IP发送消息成功,将map中的"IP"键替换为该mac,此后才能继续打开IP输入窗口
void GuiBehind::reSaveCw(QString ip_mac){
// 拆分ip和mac
QString ip = "";
QString mac = "";
QStringList sectionList = ip_mac.split(" ");
if (sectionList.size() == 1) {
mac = sectionList[0];
}
else {
ip = sectionList[0];
mac = sectionList[1];
}
qDebug() << "reSaveCw mac: " << mac;
if(this->cws.contains(mac) == false){
ChatWidget *cw = this->cws.value("MAC");
this->cws.insert(mac, cw);
}
this->cws.remove("MAC");
}
// 激活头像动画提醒
void GuiBehind::newMsgAlert(QString mac){
// 没有打开与该用户的对话框
if(this->cws.contains(mac) == false || this->cws.value(mac)->isHidden() == true){
QStandardItem *item = mBuddiesList.buddyByMac(mac);
if(item != NULL){
// 激活头像提示
item->setData(true, BuddyListItemModel::ShowMsgAnim);
}
// 激活任务栏提示
this->alertTimer->start();
//TODO: check QtQuick 2 alert
// QApplication::alert(mView, 5000);
}
}
// 更改接收文件目录
void GuiBehind::changeDestinationFolder(){
// QString dirname = QFileDialog::getExistingDirectory(mView, tr("Change folder"), ".",
// QString dirname = QFileDialog::getExistingDirectory(mView, "更改目录", ".",
QString dirname = QFileDialog::getExistingDirectory(nullptr, tr("Change folder"), ".",
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (dirname == "") return;
// 将所选路径设置为当前程序环境默认路径
QDir::setCurrent(dirname);
// 保存路径
setCurrentPath(dirname);
}
// 保存路径
void GuiBehind::setCurrentPath(QString path){
if (path == mSettings->currentPath()) return;
mSettings->savePath(path);
emit currentPathChanged();
}
// 使用文件管理器打开文件接收目录
void GuiBehind::openDestinationFolder(){
QDesktopServices::openUrl(QUrl::fromLocalFile(QDir::currentPath()));
}
// 刷新本机IP
void GuiBehind::refreshIpList(){
mIpAddresses.refreshIpList();
}
/*
* Summary: change theme color
* Parameters:
* color: target color
* Return :
*/
void GuiBehind::changeThemeColor(QString color){
mTheme.setThemeColor(color);
mSettings->saveThemeColor(color);
// 将所有的聊天窗口同步主题颜色
QMap<QString, ChatWidget *>::iterator iter = cws.begin();
while (iter != cws.end()) {
iter.value()->showme();
iter++;
}
}
/*
* Summary: close app
* Return :
*/
void GuiBehind::close(){
mDuktoProtocol->sayGoodbye();
}
// 获取图标路径,qml访问本地图片需使用绝对路径
QString GuiBehind::iconPath()
{
return "file://" + mSettings->iconPath();
}
// 保存图标路径
void GuiBehind::setIconPath(QString path)
{
if (path == mSettings->iconPath())
return;
mSettings->saveIconPath(path);
emit iconPathChanged();
}
// 获取应用版本号
QString GuiBehind::appVersion()
{
return qApp->applicationVersion();
}
/*
* Summary: say hello
* Return :
*/
void GuiBehind::periodicHello(){
mDuktoProtocol->sayHello(QHostAddress::Broadcast);
}
QString GuiBehind::currentPath()
{
return mSettings->currentPath();
}
// 将 byte 转换成 KB 或 MB
/*
* Parameters:
* size: size in byte
* Return :
*/
QString GuiBehind::sizeHuman(qint64 size){
QString sizeFormatted;
if (size < 1000)
sizeFormatted = QString::number(size) + " B";
else if (size < 1000000)
sizeFormatted = QString::number(size * 1.0 / 1000, 'f', 1) + " KB";
else
sizeFormatted = QString::number(size * 1.0 / 1000000, 'f', 1) + " MB";
return sizeFormatted;
}
// 窗口 icon 闪烁
void GuiBehind::msgAlert(){
// if(this->timeFlag % 2 == 0){
// this->mView->setIcon(this->iconBlank);
// }else{
// this->mView->setIcon(this->iconDukto);
// }
this->timeFlag ++;
}
// 查看用户手册
void GuiBehind::viewUserGuide()
{
if (!mDaemonIpcDbus->daemonIsNotRunning()){
//增加标题栏帮助菜单、F1快捷键打开用户手册
mDaemonIpcDbus->showGuide("messages");
}
}