mainwindow.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "mainwindow.h"
  2. #include "aboutwindow.h"
  3. #include "ui_mainwindow.h"
  4. #include <string>
  5. #include <map>
  6. #include <vector>
  7. #include <stdexcept>
  8. #include <QClipboard>
  9. #include <QMutex>
  10. #include <QCoreApplication>
  11. #include <QDir>
  12. #include <QFile>
  13. #include <QMessageBox>
  14. #include <QDebug>
  15. static std::map< unsigned long,std::vector<std::string> > ztReplies;
  16. static QMutex ztReplies_m;
  17. static void handleZTMessage(void *arg,unsigned long id,const char *line)
  18. {
  19. ztReplies_m.lock();
  20. if (*line) {
  21. ztReplies[id].push_back(std::string(line));
  22. ztReplies_m.unlock();
  23. } else { // empty lines conclude transmissions
  24. std::vector<std::string> resp(ztReplies[id]);
  25. ztReplies.erase(id);
  26. ztReplies_m.unlock();
  27. }
  28. }
  29. // Globally visible
  30. ZeroTier::Node::LocalClient *volatile zeroTierClient = (ZeroTier::Node::LocalClient *)0;
  31. MainWindow::MainWindow(QWidget *parent) :
  32. QMainWindow(parent),
  33. ui(new Ui::MainWindow)
  34. {
  35. ui->setupUi(this);
  36. this->startTimer(500);
  37. this->setEnabled(false); // first timer actually enables controls
  38. }
  39. MainWindow::~MainWindow()
  40. {
  41. delete ui;
  42. }
  43. void MainWindow::timerEvent(QTimerEvent *event)
  44. {
  45. QMainWindow::timerEvent(event);
  46. if (!this->isEnabled())
  47. this->setEnabled(true);
  48. if (!zeroTierClient) {
  49. std::string dotAuthFile((QDir::homePath() + QDir::separator() + ".zeroTierOneAuthToken").toStdString());
  50. std::string authToken;
  51. if (!ZeroTier::Utils::readFile(dotAuthFile.c_str(),authToken)) {
  52. #ifdef __APPLE__
  53. QString authHelperPath(QCoreApplication::applicationDirPath() + "/../Applications/ZeroTier One (Authenticate).app");
  54. if (!QFile::exists(authHelperPath)) {
  55. // Allow this to also work from the source tree if it's run from there.
  56. // This is for debugging purposes but shouldn't harm the live release
  57. // in any way.
  58. //authHelperPath = QCoreApplication::applicationFilePath() + "/../ZeroTierUI/helpers/mac/ZeroTier One (Authenticate).app";
  59. if (!QFile::exists(authHelperPath)) {
  60. QMessageBox::critical(this,"Unable to Locate Helper","Unable to locate authorization helper, cannot obtain authentication token.",QMessageBox::Ok,QMessageBox::NoButton);
  61. QApplication::exit(1);
  62. }
  63. }
  64. #endif
  65. }
  66. }
  67. }
  68. void MainWindow::on_joinNetworkButton_clicked()
  69. {
  70. }
  71. void MainWindow::on_actionAbout_triggered()
  72. {
  73. AboutWindow *about = new AboutWindow(this);
  74. about->show();
  75. }
  76. void MainWindow::on_actionJoin_Network_triggered()
  77. {
  78. // Does the same thing as clicking join button on main UI
  79. on_joinNetworkButton_clicked();
  80. }
  81. void MainWindow::on_actionShow_Detailed_Status_triggered()
  82. {
  83. }
  84. void MainWindow::on_networkIdLineEdit_textChanged(const QString &text)
  85. {
  86. QString newText;
  87. for(QString::const_iterator i(text.begin());i!=text.end();++i) {
  88. switch(i->toLatin1()) {
  89. case '0': newText.append('0'); break;
  90. case '1': newText.append('1'); break;
  91. case '2': newText.append('2'); break;
  92. case '3': newText.append('3'); break;
  93. case '4': newText.append('4'); break;
  94. case '5': newText.append('5'); break;
  95. case '6': newText.append('6'); break;
  96. case '7': newText.append('7'); break;
  97. case '8': newText.append('8'); break;
  98. case '9': newText.append('9'); break;
  99. case 'a': newText.append('a'); break;
  100. case 'b': newText.append('b'); break;
  101. case 'c': newText.append('c'); break;
  102. case 'd': newText.append('d'); break;
  103. case 'e': newText.append('e'); break;
  104. case 'f': newText.append('f'); break;
  105. case 'A': newText.append('a'); break;
  106. case 'B': newText.append('b'); break;
  107. case 'C': newText.append('c'); break;
  108. case 'D': newText.append('d'); break;
  109. case 'E': newText.append('e'); break;
  110. case 'F': newText.append('f'); break;
  111. default: break;
  112. }
  113. }
  114. ui->networkIdLineEdit->setText(newText);
  115. }
  116. void MainWindow::on_statusAndAddressButton_clicked()
  117. {
  118. // QApplication::clipboard()->setText(ui->myAddressCopyButton->text());
  119. }