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 14 kB

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

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

Contributors (1)