summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/installwindow.cpp
blob: 7a78154561319d2657a88db5b5e1163e5c4365e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 *   Copyright (C) 2007 by Dominik Riebeling
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "installwindow.h"
#include "ui_installwindowfrm.h"
#include "system.h"
#include "rbsettings.h"
#include "serverinfo.h"
#include "systeminfo.h"
#include "utils.h"
#include "rockboxinfo.h"
#include "ziputil.h"

InstallWindow::InstallWindow(QWidget *parent) : QDialog(parent)
{
    ui.setupUi(this);

    connect(ui.radioStable, SIGNAL(toggled(bool)), this, SLOT(setDetailsStable(bool)));
    connect(ui.radioCurrent, SIGNAL(toggled(bool)), this, SLOT(setDetailsCurrent(bool)));
    connect(ui.changeBackup, SIGNAL(pressed()), this, SLOT(changeBackupPath()));
    connect(ui.backup, SIGNAL(stateChanged(int)), this, SLOT(backupCheckboxChanged(int)));

    //! check if rockbox is already installed
    RockboxInfo rbinfo(RbSettings::value(RbSettings::Mountpoint).toString());
    QString version = rbinfo.version();

    if(version != "")
    {
        ui.Backupgroup->show();
        m_backupName = RbSettings::value(RbSettings::Mountpoint).toString();
        if(!m_backupName.endsWith("/")) m_backupName += "/";
        m_backupName += ".backup/rockbox-backup-"+version+".zip";
        // for some reason the label doesn't return its final size yet.
        // Delay filling ui.backupLocation until the checkbox is changed.
    }
    else
    {
        ui.Backupgroup->hide();
    }
    backupCheckboxChanged(Qt::Unchecked);


    if(ServerInfo::value(ServerInfo::CurReleaseVersion).toString().isEmpty()) {
        ui.radioStable->setEnabled(false);
    }

    // try to use the old selection first. If no selection has been made
    // in the past, use a preselection based on released status.
    if(RbSettings::value(RbSettings::Build).toString() == "stable"
        && !ServerInfo::value(ServerInfo::CurReleaseVersion).toString().isEmpty())
        ui.radioStable->setChecked(true);
    else if(RbSettings::value(RbSettings::Build).toString() == "current")
        ui.radioCurrent->setChecked(true);
    else if(!ServerInfo::value(ServerInfo::CurReleaseVersion).toString().isEmpty()) {
        ui.radioStable->setChecked(true);
        ui.radioStable->setEnabled(true);
    }
    else {
        ui.radioCurrent->setChecked(true);
        ui.radioStable->setEnabled(false);
        ui.radioStable->setChecked(false);
    }

}


void InstallWindow::resizeEvent(QResizeEvent *e)
{
    (void)e;

    // recalculate width of elided text.
    updateBackupLocation();
}


void InstallWindow::updateBackupLocation(void)
{
    ui.backupLocation->setText(QDir::toNativeSeparators(
        fontMetrics().elidedText(tr("Backup to %1").arg(m_backupName),
        Qt::ElideMiddle, ui.backupLocation->size().width())));
}


void InstallWindow::backupCheckboxChanged(int state)
{
    if(state == Qt::Checked)
    {
        ui.backupLocation->show();
        ui.changeBackup->show();
        // update backup location display.
        updateBackupLocation();
    }
    else
    {
        ui.backupLocation->hide();
        ui.changeBackup->hide();
    }
}


void InstallWindow::accept()
{
    logger = new ProgressLoggerGui(this);
    logger->show();
    QString mountPoint = RbSettings::value(RbSettings::Mountpoint).toString();
    qDebug() << "[Install] mountpoint:" << RbSettings::value(RbSettings::Mountpoint).toString();
    // show dialog with error if mount point is wrong
    if(!QFileInfo(mountPoint).isDir()) {
        logger->addItem(tr("Mount point is wrong!"),LOGERROR);
        logger->setFinished();
        return;
    }

    QString myversion;
    QString buildname = SystemInfo::value(SystemInfo::CurBuildserverModel).toString();
    if(ui.radioStable->isChecked()) {
        file = SystemInfo::value(SystemInfo::ReleaseUrl).toString();
        RbSettings::setValue(RbSettings::Build, "stable");
        myversion = ServerInfo::value(ServerInfo::CurReleaseVersion).toString();
    }
    else if(ui.radioCurrent->isChecked()) {
        file = SystemInfo::value(SystemInfo::BleedingUrl).toString();
        RbSettings::setValue(RbSettings::Build, "current");
        myversion = "r" + ServerInfo::value(ServerInfo::BleedingRevision).toString();
    }
    else {
        qDebug() << "[Install] no build selected -- this shouldn't happen";
        return;
    }
    file.replace("%MODEL%", buildname);
    file.replace("%RELVERSION%", ServerInfo::value(ServerInfo::CurReleaseVersion).toString());

    RbSettings::sync();

    QString warning = Utils::checkEnvironment(false);
    if(!warning.isEmpty())
    {
        if(QMessageBox::warning(this, tr("Really continue?"), warning,
            QMessageBox::Ok | QMessageBox::Abort, QMessageBox::Abort)
            == QMessageBox::Abort)
        {
            logger->addItem(tr("Aborted!"),LOGERROR);
            logger->setFinished();
            return;
        }
    }

    //! check if we should backup
    if(ui.backup->isChecked())
    {
        logger->addItem(tr("Beginning Backup..."),LOGINFO);
        QCoreApplication::processEvents();

        //! create dir, if it doesnt exist
        QFileInfo backupFile(m_backupName);
        if(!QDir(backupFile.path()).exists())
        {
            QDir a;
            a.mkpath(backupFile.path());
        }

        //! create backup
        bool result = true;
        ZipUtil zip(this);
        connect(&zip, SIGNAL(logProgress(int, int)), logger, SLOT(setProgress(int, int)));
        connect(&zip, SIGNAL(logItem(QString, int)), logger, SLOT(addItem(QString, int)));
        zip.open(m_backupName, QuaZip::mdCreate);
        QString mp = RbSettings::value(RbSettings::Mountpoint).toString();
        QString folder = mp + "/.rockbox";
        result = zip.appendDirToArchive(folder, mp);
        zip.close();
        if(result) {
            logger->addItem(tr("Backup finished."), LOGINFO);
        }
        else {
            logger->addItem(tr("Backup failed!"), LOGERROR);
            logger->setFinished();
            return;
        }
    }

    //! install build
    installer = new ZipInstaller(this);
    installer->setUrl(file);
    installer->setLogSection("Rockbox (Base)");
    if(!RbSettings::value(RbSettings::CacheDisabled).toBool()
        && !ui.checkBoxCache->isChecked())
    {
        installer->setCache(true);
    }
    installer->setLogVersion(myversion);
    installer->setMountPoint(mountPoint);

    connect(installer, SIGNAL(done(bool)), this, SLOT(done(bool)));

    connect(installer, SIGNAL(logItem(QString, int)), logger, SLOT(addItem(QString, int)));
    connect(installer, SIGNAL(logProgress(int, int)), logger, SLOT(setProgress(int, int)));
    connect(installer, SIGNAL(done(bool)), logger, SLOT(setFinished()));
    connect(logger, SIGNAL(aborted()), installer, SLOT(abort()));
    installer->install();

}

void InstallWindow::changeBackupPath()
{
    QString backupString = QFileDialog::getSaveFileName(this,
        tr("Select Backup Filename"), m_backupName, "*.zip");
    // only update if a filename was entered, ignore if cancelled
    if(!backupString.isEmpty()) {
        m_backupName = backupString;
        updateBackupLocation();
    }
}


// Zip installer has finished
void InstallWindow::done(bool error)
{
    qDebug() << "[Install] done, error:" << error;

    if(error)
    {
        logger->setFinished();
        return;
    }

    // no error, close the window, when the logger is closed
    connect(logger,SIGNAL(closed()),this,SLOT(close()));
    // add platform info to log file for later detection
    QSettings installlog(RbSettings::value(RbSettings::Mountpoint).toString()
            + "/.rockbox/rbutil.log", QSettings::IniFormat, 0);
    installlog.setValue("platform", RbSettings::value(RbSettings::Platform).toString());
    installlog.sync();
}


void InstallWindow::setDetailsCurrent(bool show)
{
    if(show) {
        ui.labelDetails->setText(tr("This is the absolute up to the minute "
                "Rockbox built. The development version will get updated every time "
                "a change is made. Latest development version is %1 (%2).")
                .arg(ServerInfo::value(ServerInfo::BleedingRevision).toString(),
                    ServerInfo::value(ServerInfo::BleedingDate).toString()));
    }
}


void InstallWindow::setDetailsStable(bool show)
{
    if(show) {
        ui.labelDetails->setText(
            tr("This is the last released version of Rockbox."));

        if(!ServerInfo::value(ServerInfo::CurReleaseVersion).toString().isEmpty())
            ui.labelNote->setText(tr("<b>Note:</b> "
            "The lastest stable version is %1.")
                    .arg(ServerInfo::value(ServerInfo::CurReleaseVersion).toString()));
        else ui.labelNote->setText("");
    }
}


void InstallWindow::changeEvent(QEvent *e)
{
    if(e->type() == QEvent::LanguageChange) {
        ui.retranslateUi(this);
    } else {
        QWidget::changeEvent(e);
    }
}