1 /* 2 * Copyright (c) 2022 Unionman Technology Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef MAINWINDOW_H 17 #define MAINWINDOW_H 18 19 #include "JQHttpServer.h" 20 #include "StbItemWidget.h" 21 #include <QCloseEvent> 22 #include <QDebug> 23 #include <QDialog> 24 #include <QDialogButtonBox> 25 #include <QFile> 26 #include <QFormLayout> 27 #include <QLineEdit> 28 #include <QList> 29 #include <QMainWindow> 30 #include <QMessageBox> 31 #include <QPushButton> 32 #include <QString> 33 #include <QTcpServer> 34 #include <QTcpSocket> 35 #include <QThread> 36 #include <QTimerEvent> 37 #include <QUdpSocket> 38 39 namespace Ui { 40 class MainWindow; 41 } 42 43 class MyEvent : public QEvent { 44 public: MyEvent(int m)45 explicit MyEvent(int m) 46 :QEvent(MY_EVENT_TYPE), msgType(m) 47 { 48 } MyEvent(int m,QMap<QString,QString> a)49 MyEvent(int m, QMap<QString, QString> a) 50 :QEvent(MY_EVENT_TYPE), msgType(m), args(a) 51 { 52 } 53 54 public: 55 int msgType; 56 QMap<QString, QString> args; 57 58 public: 59 static Type MY_EVENT_TYPE; 60 }; 61 62 class UpgradePackageInfo { 63 public: 64 QString filename; 65 QString md5; 66 QString swVersion; 67 QString hwVersion; 68 QString deviceModel; 69 }; 70 71 class DiscoverThread : public QThread { 72 Q_OBJECT 73 public: DiscoverThread(QObject * parent)74 DiscoverThread(QObject* parent) 75 : QThread(parent) 76 { 77 } 78 bool startUpgrade(QString url); 79 bool stopUpgrade(); 80 bool exitThread(); 81 void setDirectConnectSetupFlag(bool directConnectSetupFlag); 82 void setDirectConnectSetup(QString ip, QString mask); 83 signals: 84 void threadResult(); 85 86 protected: 87 void run(); 88 void handleDatagram(const QByteArray &datagram, const QHostAddress &clientAddress, quint16 clientPort); 89 private slots: 90 91 private: 92 bool mExitFlag = false; 93 bool mUpgradeFlag = false; 94 bool mDirectConnectSetupFlag = false; 95 QString mDirectConnectSetIp; 96 QString mDirectConnectSetSubnetMask; 97 QString mUpgradeUrl = ""; 98 }; 99 100 class MD5CalculationThread : public QThread { 101 Q_OBJECT 102 103 public: 104 explicit MD5CalculationThread(const QString& filePath, QObject* parent = nullptr) QThread(parent)105 :QThread(parent), mFilePath(filePath) 106 { 107 } 108 109 signals: 110 void md5CalculationFinished(const QString& md5); 111 112 protected: run()113 void run() override 114 { 115 QFile file(mFilePath); 116 if (!file.open(QIODevice::ReadOnly)) { 117 qDebug() << "Failed to open package file"; 118 emit md5CalculationFinished(""); 119 return; 120 } 121 122 QCryptographicHash hash(QCryptographicHash::Md5); 123 124 QByteArray data; 125 while (!file.atEnd()) { 126 data = file.read(8192L); 127 hash.addData(data); 128 } 129 130 file.close(); 131 QByteArray md5 = hash.result().toHex(); 132 emit md5CalculationFinished(QString(md5)); 133 } 134 135 private: 136 QString mFilePath; 137 }; 138 139 class MainWindow : public QMainWindow { 140 Q_OBJECT 141 142 public: 143 explicit MainWindow(QWidget* parent = nullptr); 144 ~MainWindow(); 145 QString mPackageFilePath; 146 QString mPackageFileMd5; 147 QUdpSocket* mUdpSocket = nullptr; 148 QString hostIpAddress; 149 QString subnetMask; 150 151 protected: 152 void closeEvent(QCloseEvent* e); 153 void customEvent(QEvent* event); 154 void resizeEvent(QResizeEvent* event); 155 void openDirectConnectionSetupPage(); 156 void showErrorMessage(const QString &title, const QString &message); 157 void openNetworkAdapterSettings(); 158 void handleMD5CalculationFinished(const QString& md5); 159 QString getSubnetMask(const QHostAddress& ipAddress); 160 161 private slots: 162 void disableUIElements(); 163 void on_startButton_clicked(); 164 void on_stopButton_clicked(); 165 void on_selPackageButton_clicked(); 166 void calculateAndDisplayFileSize(const QString& filePath); 167 168 void on_localIpComboBox_currentIndexChanged(const QString& arg1); 169 170 void on_refrehIpButton_clicked(); 171 void newTcpConnection(); 172 void tcpReadyRead(); 173 void tcpDisconnection(); 174 175 void on_toolMenuButton_clicked(); 176 177 void on_md5Enable_stateChanged(int arg1); 178 179 private: 180 void timerEvent(QTimerEvent* e); 181 QStringList getLocalIPs(); 182 bool startHttpServer(); 183 void stopHttpServer(); 184 bool startMonitor(); 185 void stopMonitor(); 186 bool jsonToMap(QString jsonStr, QMap<QString, QString>& map); 187 QString readFileFromZip(QString zippath, QString filepath); 188 bool hasFileInZip(QString zippath, QString filepath); 189 StbItemWidget* takeStbItemWidget(QString mac); 190 StbItemWidget* getStbItemWidgetByIp(QString ip); 191 void updateLocalIpComboBox(); 192 QMap<QString, QString> parsePropertyText(QString text); 193 194 Ui::MainWindow* ui; 195 QMap<QString, StbItemWidget*> stbItemMap; 196 int timerId = -1; 197 UpgradePackageInfo packageInfo; 198 QString mUpgradeRequestUrlPath; 199 DiscoverThread* mDiscoverThread = nullptr; 200 JQHttpServer::TcpServerManage* mHttpServer = nullptr; 201 bool mUpgradeStartFlag = false; 202 QTcpServer* mTcpServer = nullptr; 203 bool md5Enable = true; 204 }; 205 206 class DirectConnectionSetupDialog : public QDialog { 207 public: 208 explicit DirectConnectionSetupDialog(QWidget* parent = nullptr) QDialog(parent)209 : QDialog(parent) 210 { 211 setWindowTitle("直连配网界面"); 212 213 QVBoxLayout* mainLayout = new QVBoxLayout(this); 214 QFormLayout* formLayout = new QFormLayout; 215 mainLayout->addLayout(formLayout); 216 217 ipAddressLineEdit = new QLineEdit(this); 218 subnetMaskLineEdit = new QLineEdit(this); 219 ipAddressLineEdit->setMinimumSize(150L, 25L); 220 subnetMaskLineEdit->setMinimumSize(150L, 25L); 221 subnetMaskLineEdit->setEnabled(false); 222 223 formLayout->addRow("IP地址:", ipAddressLineEdit); 224 formLayout->addRow("子网掩码:", subnetMaskLineEdit); 225 226 QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); 227 mainLayout->addWidget(buttonBox); 228 mainLayout->setSpacing(10L); 229 230 connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); 231 connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 232 233 setFixedSize(260L, 120L); 234 } 235 getIpAddress()236 QString getIpAddress() const 237 { 238 return ipAddressLineEdit->text(); 239 } getSubnetMask()240 QString getSubnetMask() const 241 { 242 return subnetMaskLineEdit->text(); 243 } setIpAddressLineEdit(QString ipAddress)244 void setIpAddressLineEdit(QString ipAddress) 245 { 246 ipAddressLineEdit->setText(ipAddress); 247 } setSubnetMaskLineEdit(QString subnetMask)248 void setSubnetMaskLineEdit(QString subnetMask) 249 { 250 subnetMaskLineEdit->setText(subnetMask); 251 } getAccepted()252 bool getAccepted() const 253 { 254 return this->accepted; 255 } 256 257 private: 258 QLineEdit* ipAddressLineEdit; 259 QLineEdit* subnetMaskLineEdit; 260 261 bool accepted = false; accept()262 void accept() override 263 { 264 accepted = true; 265 QDialog::accept(); 266 } reject()267 void reject() override 268 { 269 accepted = false; 270 QDialog::reject(); 271 } 272 }; 273 274 #endif // MAINWINDOW_H 275