You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

mainwindow.cpp 16 kB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <thread>
  4. #include <QDir>
  5. #include <QDebug>
  6. #include <QMessageBox>
  7. #include <QTextCodec>
  8. #include <QFileDialog>
  9. #include <QDateTime>
  10. #include <QtMac>
  11. #include "addfellowdialog.h"
  12. #include <QProcess>
  13. #include "platformdepend.h"
  14. #include "feiqwin.h"
  15. MainWindow::MainWindow(QWidget *parent) :
  16. QMainWindow(parent),
  17. ui(new Ui::MainWindow)
  18. {
  19. ui->setupUi(this);
  20. qRegisterMetaType<shared_ptr<ViewEvent>>("ViewEventSharedPtr");
  21. connect(this, SIGNAL(showErrorAndQuit(QString)), this, SLOT(onShowErrorAndQuit(QString)));
  22. //加载配置
  23. auto settingFilePath = QDir::home().filePath(".feiq_setting.ini");
  24. mSettings = new Settings(settingFilePath, QSettings::IniFormat);
  25. mSettings->setIniCodec(QTextCodec::codecForName("UTF-8"));
  26. mTitle = mSettings->value("app/title", "mac飞秋").toString();
  27. setWindowTitle(mTitle);
  28. //初始化搜索对话框
  29. mSearchFellowDlg = new SearchFellowDlg(this);
  30. connect(mSearchFellowDlg, SIGNAL(onFellowSelected(const Fellow*)),
  31. this, SLOT(finishSearch(const Fellow*)));
  32. connect(ui->actionRefreshFellows, SIGNAL(triggered(bool)), this, SLOT(refreshFellowList()));
  33. connect(ui->actionAddFellow, SIGNAL(triggered(bool)), this, SLOT(addFellow()));
  34. mSearchFellowDlg->setSearchDriver(std::bind(&MainWindow::fellowSearchDriver, this, placeholders::_1));
  35. //初始化文件管理对话框
  36. mDownloadFileDlg = new FileManagerDlg(this);
  37. mDownloadFileDlg->setEngine(&mFeiq);
  38. connect(this, SIGNAL(statChanged(FileTask*)), mDownloadFileDlg, SLOT(statChanged(FileTask*)));
  39. connect(this, SIGNAL(progressChanged(FileTask*)), mDownloadFileDlg, SLOT(progressChanged(FileTask*)));
  40. //初始化好友列表
  41. mFellowList.bindTo(ui->fellowListWidget);
  42. connect(&mFellowList, SIGNAL(select(const Fellow*)), this, SLOT(openChartTo(const Fellow*)));
  43. //初始化接收文本框
  44. mRecvTextEdit = ui->recvEdit;
  45. connect(mRecvTextEdit, SIGNAL(navigateToFileTask(IdType,IdType,bool)), this, SLOT(navigateToFileTask(IdType,IdType,bool)));
  46. //初始化发送文本框
  47. mSendTextEdit = ui->sendEdit;
  48. connect(mSendTextEdit, SIGNAL(acceptDropFiles(QList<QFileInfo>)), this, SLOT(sendFiles(QList<QFileInfo>)));
  49. if (mSettings->value("app/send_by_enter", true).toBool())
  50. {
  51. connect(mSendTextEdit, SIGNAL(enterPressed()), this, SLOT(sendText()));
  52. connect(mSendTextEdit, SIGNAL(ctrlEnterPressed()), mSendTextEdit, SLOT(newLine()));
  53. }
  54. else
  55. {
  56. connect(mSendTextEdit, SIGNAL(ctrlEnterPressed()), this, SLOT(sendText()));
  57. connect(mSendTextEdit, SIGNAL(enterPressed()), mSendTextEdit, SLOT(newLine()));
  58. }
  59. //初始化Emoji对话框
  60. mChooseEmojiDlg = new ChooseEmojiDlg(this);
  61. connect(ui->actionInsertEmoji, SIGNAL(triggered(bool)), this, SLOT(openChooseEmojiDlg()));
  62. connect(mChooseEmojiDlg, SIGNAL(choose(QString)),mSendTextEdit, SLOT(insertPlainText(QString)));
  63. //初始化菜单
  64. connect(ui->actionSearchFellow, SIGNAL(triggered(bool)), this, SLOT(openSearchDlg()));
  65. connect(ui->actionSettings, SIGNAL(triggered(bool)), this, SLOT(openSettings()));
  66. connect(ui->actionOpendl, SIGNAL(triggered(bool)), this, SLOT(openDownloadDlg()));
  67. connect(ui->actionSendText, SIGNAL(triggered(bool)), this, SLOT(sendText()));
  68. connect(ui->actionSendKnock, SIGNAL(triggered(bool)), this, SLOT(sendKnock()));
  69. connect(ui->actionSendFile, SIGNAL(triggered(bool)), this, SLOT(sendFile()));
  70. //初始化飞秋引擎
  71. connect(this, SIGNAL(feiqViewEvent(shared_ptr<ViewEvent>)), this, SLOT(handleFeiqViewEvent(shared_ptr<ViewEvent>)));
  72. //后台初始化通信
  73. std::thread thd(&MainWindow::initFeiq, this);
  74. thd.detach();
  75. }
  76. MainWindow::~MainWindow()
  77. {
  78. mFeiq.stop();
  79. mSettings->sync();
  80. delete mSettings;
  81. delete mSearchFellowDlg;
  82. delete mDownloadFileDlg;
  83. delete mChooseEmojiDlg;
  84. delete ui;
  85. }
  86. void MainWindow::setFeiqWin(FeiqWin *feiqWin)
  87. {
  88. mFeiqWin = feiqWin;
  89. mFeiqWin->init(this);
  90. }
  91. void MainWindow::enterEvent(QEvent *event)
  92. {
  93. auto fellow = mRecvTextEdit->curFellow();
  94. if (fellow)
  95. {
  96. flushUnread(fellow);
  97. updateUnread(fellow);
  98. }
  99. PlatformDepend::instance().hideAllNotify();
  100. }
  101. void MainWindow::openChartTo(const Fellow *fellow)
  102. {
  103. mFellowList.top(*fellow);
  104. mRecvTextEdit->setCurFellow(fellow);
  105. setWindowTitle(mTitle + " - 与"+fellow->getName().c_str()+"会话中");
  106. flushUnread(fellow);
  107. updateUnread(fellow);
  108. }
  109. shared_ptr<Fellow> MainWindow::checkCurFellow()
  110. {
  111. auto fellow = mFeiq.getModel().getShared(mRecvTextEdit->curFellow());
  112. if (fellow == nullptr)
  113. {
  114. mRecvTextEdit->addWarning("这是要发给谁?");
  115. }
  116. return fellow;
  117. }
  118. void MainWindow::showResult(pair<bool, string> ret, const Content* content)
  119. {
  120. if (ret.first)
  121. mRecvTextEdit->addMyContent(content, QDateTime::currentDateTime().currentMSecsSinceEpoch());
  122. else
  123. mRecvTextEdit->addWarning(ret.second.c_str());
  124. }
  125. void MainWindow::onStateChanged(FileTask *fileTask)
  126. {
  127. if (fileTask->getState()==FileTaskState::Finish)
  128. {
  129. auto title = QString(fileTask->getTaskTypeDes().c_str())+"完成";
  130. PlatformDepend::instance().showNotify(title, fileTask->getContent()->filename.c_str());
  131. }
  132. else if (fileTask->getState()==FileTaskState::Error)
  133. {
  134. auto title = QString(fileTask->getTaskTypeDes().c_str())+"失败";
  135. auto content = QString(fileTask->getContent()->filename.c_str());
  136. content += "\n";
  137. content += fileTask->getDetailInfo().c_str();
  138. PlatformDepend::instance().showNotify(title, content);
  139. }
  140. if (mDownloadFileDlg->isVisible())
  141. {
  142. emit statChanged(fileTask);
  143. }
  144. }
  145. void MainWindow::onProgress(FileTask *fileTask)
  146. {
  147. if (mDownloadFileDlg->isVisible())
  148. {
  149. emit progressChanged(fileTask);
  150. }
  151. }
  152. void MainWindow::onEvent(shared_ptr<ViewEvent> event)
  153. {
  154. emit feiqViewEvent(event);
  155. }
  156. void MainWindow::onShowErrorAndQuit(const QString &text)
  157. {
  158. QMessageBox::warning(this, "出错了,为什么?你猜!", text, "退出应用");
  159. QApplication::exit(-1);
  160. }
  161. void MainWindow::handleFeiqViewEvent(shared_ptr<ViewEvent> event)
  162. {
  163. if (event->what == ViewEventType::FELLOW_UPDATE)
  164. {
  165. auto e = static_cast<FellowViewEvent*>(event.get());
  166. mFellowList.update(*(e->fellow.get()));
  167. }
  168. else if (event->what == ViewEventType::SEND_TIMEO || event->what == ViewEventType::MESSAGE)
  169. {
  170. //地球人都知道这个分支中的ViewEvent继承自FellowViewEvent
  171. auto e = static_cast<FellowViewEvent*>(event.get());
  172. auto fellow = e->fellow.get();
  173. if (isActiveWindow())
  174. {//窗口可见,处理当前用户消息,其他用户消息则放入通知队列
  175. if (fellow == mRecvTextEdit->curFellow())
  176. {
  177. readEvent(event.get());
  178. }
  179. else
  180. {
  181. mUnreadEvents[fellow].push_back(event);
  182. updateUnread(fellow);
  183. }
  184. }
  185. else
  186. {//窗口不可见,放入未读队列并通知
  187. mUnreadEvents[fellow].push_back(event);
  188. updateUnread(fellow);
  189. notifyUnread(event.get());
  190. }
  191. }
  192. }
  193. void MainWindow::refreshFellowList()
  194. {
  195. mFeiq.sendImOnLine();
  196. }
  197. void MainWindow::addFellow()
  198. {
  199. AddFellowDialog dlg(this);
  200. if (dlg.exec() == QDialog::Accepted)
  201. {
  202. auto ip = dlg.getIp();
  203. userAddFellow(ip);
  204. }
  205. }
  206. void MainWindow::openChooseEmojiDlg()
  207. {
  208. mChooseEmojiDlg->exec();
  209. }
  210. void MainWindow::sendFiles(QList<QFileInfo> files)
  211. {
  212. auto fellow = checkCurFellow();
  213. if (!fellow)
  214. return;
  215. for (auto file : files)
  216. {
  217. if (file.isFile())
  218. {
  219. sendFile(file.absoluteFilePath().toStdString());
  220. }
  221. else
  222. {
  223. mRecvTextEdit->addWarning("不支持发送:"+file.absoluteFilePath());
  224. }
  225. }
  226. }
  227. void MainWindow::userAddFellow(QString ip)
  228. {
  229. //创建好友
  230. auto fellow = make_shared<Fellow>();
  231. fellow->setIp(ip.toStdString());
  232. fellow->setOnLine(true);
  233. mFeiq.getModel().addFellow(fellow);
  234. //添加到列表
  235. auto& ref = *(fellow.get());
  236. mFellowList.update(ref);
  237. mFellowList.top(ref);
  238. //发送在线
  239. mFeiq.sendImOnLine(fellow->getIp());
  240. }
  241. void MainWindow::notifyUnread(const ViewEvent *event)
  242. {
  243. if (event->what == ViewEventType::SEND_TIMEO)
  244. {
  245. auto e = static_cast<const SendTimeoEvent*>(event);
  246. auto fellow = e->fellow.get();
  247. showNotification(fellow, "发送超时:"+simpleTextOf(e->content.get()));
  248. }
  249. else if (event->what == ViewEventType::MESSAGE)
  250. {
  251. auto e = static_cast<const MessageViewEvent*>(event);
  252. auto fellow = e->fellow.get();
  253. for (auto content : e->contents)
  254. {
  255. showNotification(fellow, simpleTextOf(content.get()));
  256. }
  257. }
  258. }
  259. void MainWindow::showNotification(const Fellow *fellow, const QString &text)
  260. {
  261. QString content(text);
  262. if (content.length()>20)
  263. content = content.left(20)+"...";
  264. PlatformDepend::instance().showNotify(QString(fellow->getName().c_str())+":", content);
  265. }
  266. void MainWindow::navigateToFileTask(IdType packetNo, IdType fileId, bool upload)
  267. {
  268. auto task = mFeiq.getModel().findTask(packetNo, fileId, upload ? FileTaskType::Upload : FileTaskType::Download);
  269. openDownloadDlg();
  270. mDownloadFileDlg->select(task.get());
  271. }
  272. void MainWindow::sendFile(std::string filepath)
  273. {
  274. auto content = FileContent::createFileContentToSend(filepath);
  275. auto fellow = checkCurFellow();
  276. if (!fellow)
  277. return;
  278. if (content == nullptr)
  279. {
  280. mRecvTextEdit->addWarning("获取文件"+QString(filepath.c_str())+"的信息失败,不发送");
  281. }
  282. else
  283. {
  284. auto fileContent = shared_ptr<FileContent>(std::move(content));
  285. auto ret = mFeiq.send(fellow, fileContent);
  286. showResult(ret, fileContent.get());
  287. }
  288. }
  289. void MainWindow::sendFile()
  290. {
  291. auto fellow = checkCurFellow();
  292. if (!fellow)
  293. return;
  294. //文件多选
  295. QFileDialog fdlg(this, "选择要发送的文件");
  296. fdlg.setFileMode(QFileDialog::ExistingFiles);
  297. if (fdlg.exec() == QDialog::Accepted)
  298. {
  299. auto list = fdlg.selectedFiles();
  300. auto count = list.count();
  301. for (int i = 0; i < count; i++)
  302. {
  303. auto path = list.at(i);
  304. sendFile(path.toStdString());
  305. }
  306. }
  307. }
  308. void MainWindow::sendKnock()
  309. {
  310. auto fellow = checkCurFellow();
  311. if (fellow)
  312. {
  313. auto content = make_shared<KnockContent>();
  314. auto ret = mFeiq.send(fellow, content);
  315. showResult(ret, content.get());
  316. }
  317. }
  318. void MainWindow::sendText()
  319. {
  320. auto text = mSendTextEdit->toPlainText();
  321. if (text.isEmpty())
  322. {
  323. mRecvTextEdit->addWarning("发送空文本是不科学的,驳回");
  324. return;
  325. }
  326. auto fellow = checkCurFellow();
  327. if (fellow)
  328. {
  329. auto content = make_shared<TextContent>();
  330. content->text = text.toStdString();
  331. auto ret = mFeiq.send(fellow, content);
  332. showResult(ret, content.get());
  333. mSendTextEdit->clear();
  334. }
  335. }
  336. void MainWindow::finishSearch(const Fellow *fellow)
  337. {
  338. mFellowList.top(*fellow);
  339. openChartTo(fellow);
  340. }
  341. void MainWindow::openSettings()
  342. {
  343. QMessageBox::information(this, "设置", "设置文件在:"+mSettings->fileName()+"\n重启后生效",
  344. QMessageBox::Ok);
  345. }
  346. void MainWindow::openSearchDlg()
  347. {
  348. mSearchFellowDlg->exec();
  349. }
  350. void MainWindow::openDownloadDlg()
  351. {
  352. mDownloadFileDlg->show();
  353. mDownloadFileDlg->raise();
  354. }
  355. vector<const Fellow *> MainWindow::fellowSearchDriver(const QString &text)
  356. {
  357. auto fellows = mFeiq.getModel().searchFellow(text.toStdString());
  358. vector<const Fellow*> result;
  359. for (auto fellow : fellows)
  360. {
  361. result.push_back(fellow.get());
  362. }
  363. return result;
  364. }
  365. void MainWindow::initFeiq()
  366. {
  367. //配置飞秋
  368. auto name = mSettings->value("user/name").toString();
  369. if (name.isEmpty())
  370. {
  371. emit showErrorAndQuit("请先打开【"+mSettings->fileName()+"】设置用户名(user/name)");
  372. return;
  373. }
  374. mFeiq.setMyName(name.toStdString());
  375. mFeiq.setMyHost(mSettings->value("user/host","feiq by cy").toString().toStdString());
  376. auto customGrroup = mSettings->value("network/custom_group", "").toString();
  377. if (!customGrroup.isEmpty())
  378. {
  379. auto list = customGrroup.split("|");
  380. for (int i = 0; i < list.size(); i++)
  381. {
  382. QString ipPrefix = list[i];
  383. if (ipPrefix.endsWith("."))
  384. {
  385. for (int j = 2; j < 254; j++)
  386. {
  387. auto ip = ipPrefix+QString::number(j);
  388. mFeiq.addToBroadcast(ip.toStdString());
  389. }
  390. }
  391. }
  392. }
  393. mFeiq.setView(this);
  394. mFeiq.enableIntervalDetect(60);
  395. //启动飞秋
  396. auto ret = mFeiq.start();
  397. if (!ret.first)
  398. {
  399. emit showErrorAndQuit(ret.second.c_str());
  400. }
  401. qDebug()<<"feiq started";
  402. }
  403. void MainWindow::updateUnread(const Fellow *fellow)
  404. {
  405. auto it = mUnreadEvents.find(fellow);
  406. if (it != mUnreadEvents.end())
  407. {
  408. auto count = (*it).second.size();
  409. if (count == 0)
  410. {
  411. mFellowList.mark(*fellow, "");
  412. }
  413. else
  414. {
  415. mFellowList.mark(*fellow, QString::number(count));
  416. }
  417. }
  418. setBadgeNumber(getUnreadCount());
  419. }
  420. int MainWindow::getUnreadCount()
  421. {
  422. auto begin = mUnreadEvents.begin();
  423. auto end = mUnreadEvents.end();
  424. auto count = 0;
  425. for (auto it = begin; it != end; it++)
  426. {
  427. count += it->second.size();
  428. }
  429. return count;
  430. }
  431. void MainWindow::flushUnread(const Fellow *fellow)
  432. {
  433. auto it = mUnreadEvents.find(fellow);
  434. if (it != mUnreadEvents.end())
  435. {
  436. auto& list = (*it).second;
  437. while (!list.empty())
  438. {
  439. auto event = list.front();
  440. readEvent(event.get());
  441. list.pop_front();
  442. }
  443. }
  444. }
  445. void MainWindow::readEvent(const ViewEvent *event)
  446. {
  447. if (event->what == ViewEventType::SEND_TIMEO)
  448. {
  449. auto e = static_cast<const SendTimeoEvent*>(event);
  450. auto simpleText = simpleTextOf(e->content.get());
  451. if (simpleText.length()>20){
  452. simpleText = simpleText.left(20)+"...";
  453. }
  454. mRecvTextEdit->addWarning("发送超时:"+simpleText);
  455. }
  456. else if (event->what == ViewEventType::MESSAGE)
  457. {
  458. auto e = static_cast<const MessageViewEvent*>(event);
  459. auto time = e->when.time_since_epoch().count();
  460. for (auto content : e->contents)
  461. {
  462. mRecvTextEdit->addFellowContent(content.get(), time);
  463. }
  464. }
  465. }
  466. void MainWindow::setBadgeNumber(int number)
  467. {
  468. PlatformDepend::instance().setBadgeNumber(number);
  469. }
  470. QString MainWindow::simpleTextOf(const Content *content)
  471. {
  472. switch (content->type()) {
  473. case ContentType::Text:
  474. return static_cast<const TextContent*>(content)->text.c_str();
  475. break;
  476. case ContentType::File:
  477. return static_cast<const FileContent*>(content)->filename.c_str();
  478. case ContentType::Knock:
  479. return "窗口抖动";
  480. default:
  481. return "***";
  482. break;
  483. }
  484. }

mac下的“飞秋”大多数只是飞鸽传书协议,而且未发现令人满意的开源项目,所以基于c++与qt实现了基础的飞秋协议。

Contributors (1)