UBUpdateDlg.cpp 5.23 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1 2 3
/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
Claudio Valerio's avatar
Claudio Valerio committed
4
 * the Free Software Foundation, either version 2 of the License, or
Claudio Valerio's avatar
Claudio Valerio committed
5 6 7 8 9 10 11 12 13 14
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
15 16 17
#include <QFileDialog>

#include "UBUpdateDlg.h"
18 19
#include "core/UBApplication.h"
#include "UBMainWindow.h"
20

21 22
#include "core/memcheck.h"

23
UBUpdateDlg::UBUpdateDlg(QWidget *parent, int nbFiles, const QString& bkpPath)
24 25 26 27 28 29 30 31 32 33 34 35 36 37
    : QDialog(parent)
    , mMainLayout(NULL)
    , mNbFilesLabel(NULL)
    , mBkpLabel(NULL)
    , mBkpPath(NULL)
    , mBrowseBttn(NULL)
    , mpDlgBttn(NULL)
    , mLayout(NULL)
    , mHLayout(NULL)
    , mStackedWidget(NULL)
    , mDialogWidget(NULL)
    , mProgressWidget(NULL)
    , mProgressLayout(NULL)
    , mProgressLabel(NULL)
38

39
{
40 41 42 43 44 45 46 47
    mDialogWidget = new QWidget(this);
    mProgressWidget = new QWidget(this);

    mStackedWidget = new QStackedWidget(this);
    mStackedWidget->addWidget(mDialogWidget);
    mStackedWidget->addWidget(mProgressWidget);

    setFixedSize(450, 110);
48 49
    setModal(true);
    setWindowTitle(tr("Document updater"));
50 51 52
    mLayout = new QVBoxLayout();
    setLayout(mLayout);

53 54
    QString str = QString::number(nbFiles);
    str.append(tr(" files require an update."));
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    mNbFilesLabel = new QLabel(mDialogWidget);
    mNbFilesLabel->setText(str);

    mLayout->addWidget(mNbFilesLabel);

    mBkpLabel = new QLabel(mDialogWidget);
    mBkpLabel->setText(tr("Backup path: "));

    mBkpPath = new QLineEdit(mDialogWidget);
    mBkpPath->setText(bkpPath);

    mBrowseBttn = new QPushButton(mDialogWidget);
    mBrowseBttn->setText(tr("Browse"));

    mHLayout = new QHBoxLayout();
    mHLayout->addWidget(mBkpLabel);
    mHLayout->addWidget(mBkpPath, 1);
    mHLayout->addWidget(mBrowseBttn);

    mLayout->addLayout(mHLayout);

    mpDlgBttn = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, mDialogWidget);
    mLayout->addWidget(mpDlgBttn);
78 79

    mpDlgBttn->button(QDialogButtonBox::Ok)->setText(tr("Update"));
80
    mpDlgBttn->button(QDialogButtonBox::Cancel)->setText(tr("Remind me later"));
81

82
    QObject::connect(mBrowseBttn, SIGNAL(clicked()), this, SLOT(onBrowse()));
83 84
    QObject::connect(mpDlgBttn, SIGNAL(accepted()), this, SLOT(onUpdate()));
    QObject::connect(mpDlgBttn, SIGNAL(rejected()), this, SLOT(reject()));
85 86 87 88 89 90
    mDialogWidget->setLayout(mLayout);
    mStackedWidget->setCurrentWidget(mDialogWidget);

    mMainLayout = new QVBoxLayout();
    this->setLayout(mMainLayout);
    mMainLayout->addWidget(mStackedWidget);
91 92 93 94
}

UBUpdateDlg::~UBUpdateDlg()
{
95
    if (NULL != mpDlgBttn)
96 97 98 99
    {
        delete mpDlgBttn;
        mpDlgBttn = NULL;
    }
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

    if (mNbFilesLabel) {
        delete mNbFilesLabel;
        mNbFilesLabel = NULL;
    }

    if (mBkpLabel) {
        delete mBkpLabel;
        mBkpLabel = NULL;
    }

    if (mBkpPath) {
        delete mBkpPath;
        mBkpPath = NULL;
    }

    if (mBrowseBttn) {
        delete mBrowseBttn;
        mBrowseBttn = NULL;
    }

    if (mProgressLabel) {
        delete mProgressLabel;
        mProgressLabel = NULL;
    }

    if (mHLayout) {
        delete mHLayout;
        mHLayout = NULL;
    }

    if (mLayout) {
        delete mLayout;
        mLayout = NULL;
    }

    if (mProgressLayout) {
        delete mProgressLayout;
        mProgressLayout = NULL;
    }

    if (mDialogWidget) {
        delete mDialogWidget;
        mDialogWidget = NULL;
    }

    if (mProgressWidget) {
        delete mProgressWidget;
        mProgressWidget = NULL;
    }

    if (mStackedWidget) {
        delete mStackedWidget;
        mStackedWidget = NULL;
    }

    if (mMainLayout) {
        delete mMainLayout;
        mMainLayout = NULL;
    }
160 161 162 163 164
}

void UBUpdateDlg::onBrowse()
{
    QString qsSelectedDir;
165 166
    qsSelectedDir = QFileDialog::getExistingDirectory(this, tr("Select a backup folder"), mBkpPath->text());
    mBkpPath->setText(qsSelectedDir);
167 168 169 170
}

void UBUpdateDlg::onUpdate()
{
171
    mProgressLabel = new QLabel(mProgressWidget);
172
    mProgressLabel->setText(tr("Please wait the import process will start soon..."));
173 174 175 176
    mProgressLayout = new QHBoxLayout();
    mProgressLayout->addWidget(mProgressLabel);
    mProgressWidget->setLayout(mProgressLayout);
    mStackedWidget->setCurrentWidget(mProgressWidget);
177 178 179 180 181
    emit updateFiles();
}

void UBUpdateDlg::onFilesUpdated(bool bResult)
{
182
    this->hide();
183 184
    QString qsMsg;

185
    if (bResult) {
186
        qsMsg = tr("Files update successful!\nPlease reboot the application to access the updated documents.");
187
    }
188
    else {
189 190
        qsMsg = tr("An error occured during the update. The files have not been affected.");
    }
191
    UBApplication::mainWindow->information(tr("Files update results"), qsMsg);
192 193 194 195
}

QString UBUpdateDlg::backupPath()
{
196
    return mBkpPath->text();
197
}
198 199 200 201 202 203

void UBUpdateDlg::transitioningFile(QString fileName)
{
    mProgressLabel->setText(tr("Updating file ") + fileName);
}