xref: /OK3568_Linux_fs/app/qsetting/qtupdate.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 
2 #include <QApplication>
3 #include <QDesktopWidget>
4 #include <QFileInfo>
5 #include <QMessageBox>
6 #include <QProcess>
7 #include "qtupdate.h"
8 
9 #define SD_UPDATE_FILE "/sdcard/update.img"
10 #define DATA_UPDATE_FILE "/userdata/update.img"
11 #define UPDATE_EXE "/usr/bin/update"
12 
qtUpdate(QWidget * parent)13 qtUpdate::qtUpdate(QWidget *parent)
14 {
15     const QRect availableGeometry = QApplication::desktop()->availableGeometry(parent);
16 
17     QString s = "Please put update.img in \n";
18     s.append(SD_UPDATE_FILE);
19     s.append("\n or \n");
20     s.append(DATA_UPDATE_FILE);
21     s.append("\n then click OK button.");
22     QFont font;
23     font.setBold(true);
24     font.setPixelSize(availableGeometry.height()/40);
25     label.setFont(font);
26     label.setText(s);
27     label.setAlignment(Qt::AlignCenter);
28     btn.setText("O K");
29     connect(&btn, SIGNAL(clicked(bool)), this, SLOT(on_btnClicked()));
30     vLayout.addWidget(&label);
31     vLayout.addWidget(&btn);
32     setLayout(&vLayout);
33     setStyleSheet("background-color:rgb(204,228,247)");
34     setObjectName("Update");
35 }
36 
~qtUpdate()37 qtUpdate::~qtUpdate()
38 {
39 
40 }
41 
on_btnClicked()42 void qtUpdate::on_btnClicked()
43 {
44     QFileInfo userdata = QFileInfo(DATA_UPDATE_FILE);
45     QFileInfo sd = QFileInfo(SD_UPDATE_FILE);
46     QFileInfo update = QFileInfo(UPDATE_EXE);
47     QString path;
48 
49     if(userdata.exists()){
50         path = DATA_UPDATE_FILE;
51     }else if(sd.exists()){
52         path = SD_UPDATE_FILE;
53     }else {
54         QMessageBox::warning(this, "Error", "Don't find update.img in " DATA_UPDATE_FILE " and " SD_UPDATE_FILE "!");
55         return;
56     }
57 
58     QMessageBox::StandardButton rb = QMessageBox::question(
59                 this, "Update",
60                 "Found update.img in " + path + ", Do you want to reboot and update it?",
61                 QMessageBox::Yes | QMessageBox::No);
62     if(rb == QMessageBox::Yes){
63         if(update.exists()){
64             QStringList slist;
65             QProcess p;
66             slist << "ota" << path;
67             p.start(UPDATE_EXE, slist);
68             p.waitForStarted();
69             p.waitForFinished();
70             QString err = QString::fromLocal8Bit(p.readAllStandardOutput());
71             QMessageBox::critical(this, "Error", err);
72         }else {
73             QMessageBox::warning(this, "Error", "Don't find " UPDATE_EXE "!");
74         }
75     }
76 }
77