summaryrefslogtreecommitdiffstats
path: root/installerbuilder/libinstaller/kdtools/KDUpdater/kdupdaterupdatesourcesview.cpp
blob: aec9426140ffb5ee4253b69b4dc8abd010852a4d (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/****************************************************************************
** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB.  All rights reserved.
**
** This file is part of the KD Tools library.
**
** Licensees holding valid commercial KD Tools licenses may use this file in
** accordance with the KD Tools Commercial License Agreement provided with
** the Software.
**
**
** This file may be distributed and/or modified under the terms of the
** GNU Lesser General Public License version 2 and version 3 as published by the
** Free Software Foundation and appearing in the file LICENSE.LGPL included.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** Contact info@kdab.com if any conditions of this licensing are not
** clear to you.
**
**********************************************************************/

#include "kdupdaterupdatesourcesview.h"
#include "kdupdaterupdatesourcesinfo.h"
#include "ui_addupdatesourcedialog.h"

#include <QMessageBox>
#include <QContextMenuEvent>
#include <QAction>
#include <QMenu>

/*!
   \ingroup kdupdater
   \class KDUpdater::UpdateSourcesView kdupdaterupdatesourcesview.h KDUpdaterUpdateSourcesView
   \brief A widget that helps view and/or edit \ref KDUpdater::UpdateSourcesInfo

   \ref KDUpdater::UpdateSourcesInfo, associated with \ref KDUpdater::Application, contains information
   about all the update sources from which the application can download and install updates.
   This widget helps view and edit update sources information.

   \image html updatesourcesview.jpg

   The widget provides the following slots for editing update sources information
   \ref addNewSource()
   \ref editCurrentSource()
   \ref removeCurrentSource()

   You can include this widget within another form or dialog and connect to these slots which make
   use of an inbuilt dialog box to add/edit update sources. Shown below is a screenshot of the
   inbuilt dialog box.

   \image html editupdatesource.jpg

   Alternatively you can also use your own dialog box and directly update \ref KDUpdater::UpdateSourcesInfo.
   This widget connects to \ref KDUpdater::UpdateSourcesInfo signals and ensures that the data it displays
   is always kept updated.

   The widget provides a context menu using which you can add/remove/edit update sources. Shown below is a
   screenshot of the context menu.

   \image html updatesourcesview_contextmenu.jpg
*/

struct KDUpdater::UpdateSourcesView::UpdateSourcesViewData
{
    UpdateSourcesViewData( UpdateSourcesView* qq ) :
        q( qq ),
        updateSourcesInfo(0)
    {}

    UpdateSourcesView* q;
    UpdateSourcesInfo* updateSourcesInfo;
};

/*!
   Constructor
*/
KDUpdater::UpdateSourcesView::UpdateSourcesView(QWidget* parent)
    : QTreeWidget(parent),
      d(new KDUpdater::UpdateSourcesView::UpdateSourcesViewData( this ) )
{
    setColumnCount(3);
    setHeaderLabels( QStringList() << tr("Name") << tr("Title") << tr("URL") );
    setRootIsDecorated(false);

    connect(this, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this, SLOT(editCurrentSource()));
}

/*!
   Destructor
*/
KDUpdater::UpdateSourcesView::~UpdateSourcesView()
{
    delete d;
}

/*!
   Sets the \ref KDUpdater::UpdateSourcesInfo object whose information this widget should show.

   \code
   KDUpdater::Application application;

   KDUpdater::UpdateSourcesView updatesView;
   updatesView.setUpdateSourcesInfo( application.updateSourcesInfo() );
   updatesView.show();
   \endcode
*/
void KDUpdater::UpdateSourcesView::setUpdateSourcesInfo(KDUpdater::UpdateSourcesInfo* info)
{
    if( d->updateSourcesInfo == info )
        return;

    if(d->updateSourcesInfo)
        disconnect(d->updateSourcesInfo, 0, this, 0);

    d->updateSourcesInfo = info;
    if(d->updateSourcesInfo)
    {
        connect(d->updateSourcesInfo, SIGNAL(reset()), this, SLOT(refresh()));
        connect(d->updateSourcesInfo, SIGNAL(updateSourceInfoAdded(UpdateSourceInfo)),
                this, SLOT(slotUpdateSourceInfoAdded(UpdateSourceInfo)));
        connect(d->updateSourcesInfo, SIGNAL(updateSourceInfoRemoved(UpdateSourceInfo)),
                this, SLOT(slotUpdateSourceInfoRemoved(UpdateSourceInfo)));
        connect(d->updateSourcesInfo, SIGNAL(updateSourceInfoChanged(UpdateSourceInfo,UpdateSourceInfo)),
                this, SLOT(slotUpdateSourceInfoChanged(UpdateSourceInfo,UpdateSourceInfo)));
    }

    refresh();
}

/*!
   Returns a pointer to the \ref KDUpdater::UpdateSourcesInfo object whose information this
   widget is showing.
*/
KDUpdater::UpdateSourcesInfo* KDUpdater::UpdateSourcesView::updateSourcesInfo() const
{
    return d->updateSourcesInfo;
}

/*!
   Returns the index of the currently selected update source in the widget. You can use this
   index along with \ref KDUpdater::UpdateSourcesInfo::updateSourceInfo() method to get hold
   of the update source info.
*/
int KDUpdater::UpdateSourcesView::currentUpdateSourceInfoIndex() const
{
    if( !d->updateSourcesInfo )
        return -1;

    QTreeWidgetItem* item = this->currentItem();
    if( !item )
        return -1;

    int index = this->indexOfTopLevelItem(item);
    if( index < 0 )
        return -1;

    return index;
}

/*!
   Call this slot to reload the updates information. By default this slot is connected to
   \ref KDUpdater::UpdateSourcesInfo::reset() signal in \ref setUpdateSourcesInfo().
*/
void KDUpdater::UpdateSourcesView::refresh()
{
    this->clear();

    if( !d->updateSourcesInfo )
        return;

    for(int i=0; i<d->updateSourcesInfo->updateSourceInfoCount(); i++)
    {
        KDUpdater::UpdateSourceInfo info = d->updateSourcesInfo->updateSourceInfo(i);
        QTreeWidgetItem* item = new QTreeWidgetItem(this);
        item->setText(0, info.name);
        item->setText(1, info.title);
        item->setText(2, info.url.toString());
        item->setData(0, Qt::UserRole, qVariantFromValue<KDUpdater::UpdateSourceInfo>(info));
    }

    resizeColumnToContents(0);
}

/*!
   Call this slot to make use of the in-built dialog box to add a new update source. Shown
   below is a screenshot of the in-built dialog box.

   \image html addupdatesource.jpg
*/
void KDUpdater::UpdateSourcesView::addNewSource()
{
    if( !d->updateSourcesInfo )
        return;

    QDialog dialog(this);
    Ui::AddUpdateSourceDialog ui;
    ui.setupUi(&dialog);

    while(1)
    {
        if( dialog.exec() == QDialog::Rejected )
            return;

        if(ui.txtName->text().isEmpty() || ui.txtUrl->text().isEmpty())
        {
            QMessageBox::information(this, tr("Invalid Update Source Info"),
                                     tr("A valid update source name and url has to be provided"));
            continue;
        }

        break;
    }

    KDUpdater::UpdateSourceInfo newInfo;
    newInfo.name = ui.txtName->text();
    newInfo.title = ui.txtTitle->text();
    newInfo.description = ui.txtDescription->toPlainText();  // FIXME: This should perhaps be toHtml
    newInfo.url = QUrl(ui.txtUrl->text());

    d->updateSourcesInfo->addUpdateSourceInfo(newInfo);
}

/*!
   Call this slot to delete the currently selected update source.
*/
void KDUpdater::UpdateSourcesView::removeCurrentSource()
{
    if( !d->updateSourcesInfo )
        return;

    QTreeWidgetItem* item = this->currentItem();
    if( !item )
        return;

    int index = this->indexOfTopLevelItem(item);
    if( index < 0 )
        return;

    d->updateSourcesInfo->removeUpdateSourceInfoAt(index);
}

/*!
   Call this slot to edit the currently selected update source, using the in-built edit
   update source dialog box. Shown below is a screenshot of the edit update source dialog
   box.

   \image html editupdatesource.jpg
*/
void KDUpdater::UpdateSourcesView::editCurrentSource()
{
    if( !d->updateSourcesInfo )
        return;

    QTreeWidgetItem* item = this->currentItem();
    if( !item )
        return;

    int index = this->indexOfTopLevelItem(item);
    if( index < 0 )
        return;

    KDUpdater::UpdateSourceInfo info = item->data(0, Qt::UserRole).value<KDUpdater::UpdateSourceInfo>();

    QDialog dialog(this);
    Ui::AddUpdateSourceDialog ui;
    ui.setupUi(&dialog);
    ui.txtName->setText(info.name);
    ui.txtTitle->setText(info.title);
    ui.txtDescription->setPlainText(info.description); // FIXME: This should perhaps be setHtml
    ui.txtUrl->setText(info.url.toString());
    dialog.setWindowTitle(tr("Edit Update Source"));

    if( dialog.exec() == QDialog::Rejected )
        return;

    KDUpdater::UpdateSourceInfo newInfo;
    newInfo.name = ui.txtName->text();
    newInfo.title = ui.txtTitle->text();
    newInfo.description = ui.txtDescription->toPlainText(); // FIXME: This should perhaps be setHtml
    newInfo.url = QUrl(ui.txtUrl->text());

    d->updateSourcesInfo->setUpdateSourceInfoAt(index, newInfo);
}

/*!
   \internal
*/
void KDUpdater::UpdateSourcesView::slotUpdateSourceInfoAdded(const KDUpdater::UpdateSourceInfo &info)
{
    if( !d->updateSourcesInfo )
        return;

    QTreeWidgetItem* item = new QTreeWidgetItem(this);
    item->setText(0, info.name);
    item->setText(1, info.title);
    item->setText(2, info.url.toString());
    item->setData(0, Qt::UserRole, qVariantFromValue<KDUpdater::UpdateSourceInfo>(info));
}

/*!
   \internal
*/
void KDUpdater::UpdateSourcesView::slotUpdateSourceInfoRemoved(const KDUpdater::UpdateSourceInfo &info)
{
    if( !d->updateSourcesInfo )
        return;

    QTreeWidgetItem* item = 0;
    for(int i=0; i<topLevelItemCount(); i++)
    {
        item = topLevelItem(i);
        KDUpdater::UpdateSourceInfo itemInfo = item->data(0, Qt::UserRole).value<KDUpdater::UpdateSourceInfo>();
        if(itemInfo == info)
            break;
        item = 0;
    }

    if( !item )
        return;

    delete item;
}

/*!
   \internal
*/
void KDUpdater::UpdateSourcesView::slotUpdateSourceInfoChanged (const KDUpdater::UpdateSourceInfo &newInfo,
                                                                const KDUpdater::UpdateSourceInfo &oldInfo)
{
    if( !d->updateSourcesInfo )
        return;

    QTreeWidgetItem* item = 0;
    for(int i=0; i<topLevelItemCount(); i++)
    {
        item = topLevelItem(i);
        KDUpdater::UpdateSourceInfo itemInfo = item->data(0, Qt::UserRole).value<KDUpdater::UpdateSourceInfo>();
        if(itemInfo == oldInfo)
            break;
        item = 0;
    }

    if( !item )
        return;

    item->setText(0, newInfo.name);
    item->setText(1, newInfo.title);
    item->setText(2, newInfo.url.toString());
    item->setData(0, Qt::UserRole, qVariantFromValue<KDUpdater::UpdateSourceInfo>(newInfo));
}

/*!
   \internal
*/
void KDUpdater::UpdateSourcesView::contextMenuEvent(QContextMenuEvent* e)
{
    QTreeWidgetItem* item = this->itemAt( e->pos() );

    QMenu menu;
    QAction* addAction = menu.addAction(tr("&Add Source"));
    QAction* editAction = item ? menu.addAction(tr("&Edit Source")) : 0;
    QAction* remAction = item ? menu.addAction(tr("&Remove Source")) : 0;

    QAction* result = menu.exec( QCursor::pos() );
    if( !result )
        return;

    if( result == addAction )
        this->addNewSource();
    else if( result == remAction )
        this->removeCurrentSource();
    else if( result == editAction )
        this->editCurrentSource();
}