mainwindow.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #include <QProcess>
  16. #include <QStringList>
  17. static std::map< unsigned long,std::vector<std::string> > ztReplies;
  18. static QMutex ztReplies_m;
  19. static void handleZTMessage(void *arg,unsigned long id,const char *line)
  20. {
  21. ztReplies_m.lock();
  22. if (*line) {
  23. ztReplies[id].push_back(std::string(line));
  24. ztReplies_m.unlock();
  25. } else { // empty lines conclude transmissions
  26. std::vector<std::string> resp(ztReplies[id]);
  27. ztReplies.erase(id);
  28. ztReplies_m.unlock();
  29. }
  30. }
  31. // Globally visible
  32. ZeroTier::Node::LocalClient *volatile zeroTierClient = (ZeroTier::Node::LocalClient *)0;
  33. MainWindow::MainWindow(QWidget *parent) :
  34. QMainWindow(parent),
  35. ui(new Ui::MainWindow)
  36. {
  37. ui->setupUi(this);
  38. this->startTimer(1000);
  39. this->setEnabled(false); // gets enabled when updates are received
  40. }
  41. MainWindow::~MainWindow()
  42. {
  43. delete ui;
  44. delete zeroTierClient;
  45. zeroTierClient = (ZeroTier::Node::LocalClient *)0;
  46. }
  47. void MainWindow::timerEvent(QTimerEvent *event)
  48. {
  49. QMainWindow::timerEvent(event);
  50. if (!zeroTierClient) {
  51. std::string dotAuthFile((QDir::homePath() + QDir::separator() + ".zeroTierOneAuthToken").toStdString());
  52. std::string authToken;
  53. if (!ZeroTier::Utils::readFile(dotAuthFile.c_str(),authToken)) {
  54. #ifdef __APPLE__
  55. // Run the little AppleScript hack that asks for admin credentials and
  56. // then installs the auth token file in the current user's home.
  57. QString authHelperPath(QCoreApplication::applicationDirPath() + "/../Resources/helpers/mac/ZeroTier One (Authenticate).app/Contents/MacOS/applet");
  58. if (!QFile::exists(authHelperPath)) {
  59. // Allow this to also work from the source tree if it's run from there.
  60. // This is for debugging purposes and shouldn't harm the live release
  61. // in any way.
  62. authHelperPath = QCoreApplication::applicationDirPath() + "/../../../../ZeroTierUI/helpers/mac/ZeroTier One (Authenticate).app/Contents/MacOS/applet";
  63. if (!QFile::exists(authHelperPath)) {
  64. QMessageBox::critical(this,"Unable to Locate Helper","Unable to locate authorization helper, cannot obtain authentication token.",QMessageBox::Ok,QMessageBox::NoButton);
  65. QApplication::exit(1);
  66. return;
  67. }
  68. }
  69. QProcess::execute(authHelperPath,QStringList());
  70. #endif
  71. if (!ZeroTier::Utils::readFile(dotAuthFile.c_str(),authToken)) {
  72. QMessageBox::critical(this,"Cannot Authorize","Unable to authorize this user to administrate ZeroTier One.\n\nTo do so manually, copy 'authtoken.secret' from the ZeroTier One home directory to '.zeroTierOneAuthToken' in your home directory and set file modes on this file to only be readable by you (e.g. 0600 on Mac or Linux systems).",QMessageBox::Ok,QMessageBox::NoButton);
  73. QApplication::exit(1);
  74. return;
  75. }
  76. }
  77. zeroTierClient = new ZeroTier::Node::LocalClient(authToken.c_str(),0,&handleZTMessage,this);
  78. }
  79. }
  80. void MainWindow::on_joinNetworkButton_clicked()
  81. {
  82. }
  83. void MainWindow::on_actionAbout_triggered()
  84. {
  85. AboutWindow *about = new AboutWindow(this);
  86. about->show();
  87. }
  88. void MainWindow::on_actionJoin_Network_triggered()
  89. {
  90. // Does the same thing as clicking join button on main UI
  91. on_joinNetworkButton_clicked();
  92. }
  93. void MainWindow::on_actionShow_Detailed_Status_triggered()
  94. {
  95. }
  96. void MainWindow::on_networkIdLineEdit_textChanged(const QString &text)
  97. {
  98. QString newText;
  99. for(QString::const_iterator i(text.begin());i!=text.end();++i) {
  100. switch(i->toLatin1()) {
  101. case '0': newText.append('0'); break;
  102. case '1': newText.append('1'); break;
  103. case '2': newText.append('2'); break;
  104. case '3': newText.append('3'); break;
  105. case '4': newText.append('4'); break;
  106. case '5': newText.append('5'); break;
  107. case '6': newText.append('6'); break;
  108. case '7': newText.append('7'); break;
  109. case '8': newText.append('8'); break;
  110. case '9': newText.append('9'); break;
  111. case 'a': newText.append('a'); break;
  112. case 'b': newText.append('b'); break;
  113. case 'c': newText.append('c'); break;
  114. case 'd': newText.append('d'); break;
  115. case 'e': newText.append('e'); break;
  116. case 'f': newText.append('f'); break;
  117. case 'A': newText.append('a'); break;
  118. case 'B': newText.append('b'); break;
  119. case 'C': newText.append('c'); break;
  120. case 'D': newText.append('d'); break;
  121. case 'E': newText.append('e'); break;
  122. case 'F': newText.append('f'); break;
  123. default: break;
  124. }
  125. }
  126. ui->networkIdLineEdit->setText(newText);
  127. }
  128. void MainWindow::on_statusAndAddressButton_clicked()
  129. {
  130. // QApplication::clipboard()->setText(ui->myAddressCopyButton->text());
  131. }