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

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

Contributors (1)