summaryrefslogtreecommitdiff
path: root/gui/ui/AddRm/PkAddRm.cpp
blob: 15b415a9862703f5faa2952a2291ee5dc67f43db (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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/***************************************************************************
 *   Copyright (C) 2008 by Daniel Nicoletti   *
 *   mirttex85-pk@yahoo.com.br   *
 *                                                                         *
 *   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  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <KLocale>
#include <KStandardDirs>
#include <QPalette>
#include <QColor>

#include "PkTransaction.h"
#include "PkAddRm.h"

#define UNIVERSAL_PADDING 6

PkAddRm::PkAddRm( QWidget *parent )
 : QWidget( parent ), m_viewWidth(0)
{
    setupUi( this );

    //initialize the model, delegate, client and  connect it's signals
    packageView->setItemDelegate(pkg_delegate = new PkDelegate(this));
    packageView->setModel(m_pkg_model_main = new PkAddRmModel(this));
    updateColumnsWidth(true);

    // Create a new daemon
    m_daemon = new Daemon(this);
qDebug() << m_daemon->getActions();
    // create the install transaction
    m_pkClient_install = m_daemon->newTransaction();
    connect( m_pkClient_install, SIGNAL(GotPackage(Package *)), m_pkg_model_main, SLOT(addPackage(Package *)) );
    connect( m_pkClient_install, SIGNAL(Finished(Exit::Value, uint)), this, SLOT(Finished(Exit::Value, uint)) );
    connect( m_pkClient_install, SIGNAL(Files(Package *, QStringList)), this, SLOT(Files(Package *, QStringList)) );
    connect( m_pkClient_install, SIGNAL( Message(const QString&, const QString&) ), this, SLOT( Message(const QString&, const QString&) ) );
    connect( m_pkClient_install, SIGNAL( ErrorCode(const QString&, const QString&) ), this, SLOT( Message(const QString&, const QString&) ) );

    // create the main transaction
    m_pkClient_main = m_daemon->newTransaction();
    connect( m_pkClient_main, SIGNAL(GotPackage(Package *)), m_pkg_model_main, SLOT(addPackage(Package *)) );
    connect( m_pkClient_main, SIGNAL(Finished(Exit::Value, uint)), this, SLOT(Finished(Exit::Value, uint)) );
    connect( m_pkClient_main, SIGNAL( Message(const QString&, const QString&) ), this, SLOT( Message(const QString&, const QString&) ) );

    //initialize the groups
    //TODO map everything and fix search group.
    for (int i = 0; i < m_daemon->getGroups().size(); ++i) {
        if ( m_daemon->getGroups().at(i) == "accessories" )
	    groupsCB->addItem(KIcon("applications-accessories"), i18n("Accessories"));
	else if ( m_daemon->getGroups().at(i) == "games" )
	    groupsCB->addItem(KIcon("applications-games"), i18n("Games"));
	else if ( m_daemon->getGroups().at(i) == "graphics" )
	    groupsCB->addItem(KIcon("applications-graphics"), i18n("Graphics"));
	else if ( m_daemon->getGroups().at(i) == "internet" )
	    groupsCB->addItem(KIcon("applications-internet"), i18n("Internet"));
	else if ( m_daemon->getGroups().at(i) == "office" )
	    groupsCB->addItem(KIcon("applications-office"), i18n("Office"));
	else if ( m_daemon->getGroups().at(i) == "other" )
	    groupsCB->addItem(KIcon("applications-other"), i18n("Other"));
	else if ( m_daemon->getGroups().at(i) == "programming" )
	    groupsCB->addItem(KIcon("applications-development"), i18n("Development"));
	else if ( m_daemon->getGroups().at(i) == "multimedia" )
	    groupsCB->addItem(KIcon("applications-multimedia"), i18n("Multimedia"));
	else if ( m_daemon->getGroups().at(i) == "system" )
	    groupsCB->addItem(KIcon("applications-system"), i18n("System"));
    }
    // install the backend filters
    FilterMenu( m_daemon->getFilters() );

    // create the description transaction
    m_pkClient_desc = m_daemon->newTransaction();
    connect( m_pkClient_desc, SIGNAL(Details(Package *,  const QString, const QString, const QString,
        const QString, qulonglong) ),
        this, SLOT( Description(Package *, const QString, const QString, const QString,
        const QString, qulonglong) ) );

    // create the files transaction
    m_pkClient_files = m_daemon->newTransaction();
    connect( m_pkClient_files, SIGNAL( Files(Package *, QStringList) ), this, SLOT( Files(Package *, QStringList) ) );

    // create a transaction for the dependecies, and its model.
    m_pkClient_dep = m_daemon->newTransaction();
    dependsOnLV->setModel(m_pkg_model_dep = new PkAddRmModel(this));
    connect( m_pkClient_dep, SIGNAL(GotPackage(Package *)), m_pkg_model_dep, SLOT(addPackage(Package *)) );
 
    // create a transaction for the requirements, and its model.
    m_pkClient_req = m_daemon->newTransaction();
    requiredByLV->setModel(m_pkg_model_req = new PkAddRmModel(this));
    connect( m_pkClient_req, SIGNAL(GotPackage(Package *)), m_pkg_model_req, SLOT(addPackage(Package *)) );

    // connect the timer...
    connect(&m_notifyT, SIGNAL(timeout()), this, SLOT(notifyUpdate()));

    infoHide();
}

void PkAddRm::resizeEvent ( QResizeEvent * event )
{
    updateColumnsWidth();

    QWidget::resizeEvent(event);
}

bool PkAddRm::event ( QEvent * event )
{
    switch (event->type()) {
        case QEvent::PolishRequest:
        case QEvent::Polish:
            updateColumnsWidth(true);
            break;
        default:
            break;
    }

    return QWidget::event(event);
}

void PkAddRm::updateColumnsWidth(bool force)
{
    m_viewWidth = packageView->viewport()->width();

    if (force) {
        m_viewWidth -= style()->pixelMetric(QStyle::PM_ScrollBarExtent) + UNIVERSAL_PADDING;
    }

    packageView->setColumnWidth(0, pkg_delegate->columnWidth(0, m_viewWidth));
    packageView->setColumnWidth(1, pkg_delegate->columnWidth(1, m_viewWidth));
}

PkAddRm::~PkAddRm()
{
    delete m_pkg_model_main;
    delete pkg_delegate;
    delete m_pkClient_main;
    delete m_toolQM;
    delete m_pkg_model_dep;
    delete m_pkClient_dep;
    delete m_pkg_model_req;
    delete m_pkClient_req;
}

void PkAddRm::infoHide()
{
    // hides the description to have more space.
    descriptionDW->setVisible(false);
    actionPB->hide();
    notifyF->hide();
    // cleans the models
    m_currPkg = 0;
    m_pkg_model_main->clear();
    m_pkg_model_req->clear();
    m_pkg_model_dep->clear();
}

void PkAddRm::infoShow()
{
    descriptionDW->setVisible(true);
    notifyF->show();
    actionPB->show();
}

void PkAddRm::on_searchPB_clicked()
{
    infoHide();
//     qDebug() << "Search Name " << filters() ;
    m_pkClient_main->searchName( filters(), lineEdit->text() );
}

void PkAddRm::Message(const QString &one, const QString &two)
{
    qDebug() << "Error code: " << one << " two: " << two;
}

void PkAddRm::on_groupsCB_currentIndexChanged( const QString & text )
{
    //TODO fix this mapping
    qDebug() << "Search Group " << text.toLower();
    infoHide();
    m_pkClient_main->searchGroup( filters(), text.toLower() );
}

void PkAddRm::on_packageView_pressed( const QModelIndex & index )
{
    m_pkClient_desc->getDetails(m_pkg_model_main->package(index));
    m_currPkg = m_pkg_model_main->package(index);
    if (m_currPkg) {
       if (m_currPkg->info() == "installed")
           actionPB->setText( i18n("Remove") );
       else
           actionPB->setText( i18n("Install") );
    }
    qDebug() << index.model()->data(index, PkAddRmModel::IdRole).toString();
}

void PkAddRm::on_actionPB_clicked()
{
packageView->resizeColumnToContents(1);
//     Transaction *trans = m_daemon->newTransaction();
//     if (m_daemon->getActions().contains("get-depends") ) {
//         trans->getDepends("~installed", m_currPkg, true);
// //     connect( m_pkClient_req, SIGNAL( GotPackage(Package *) ), m_pkg_model_req, SLOT( addPackage(Package *) ) );
// //     connect( m_pkClient_req, SIGNAL( Finished(Exit::Value, uint) ), this, SLOT( reqFinished(Exit::Value, uint) ) );
//     }
//         
//     PkAddRmTransaction *frm = new PkAddRmTransaction(, this);
//     frm->exec();
//     delete frm;
//     qDebug() << "mainEXEC()";
}

void PkAddRm::Finished(Exit::Value status, uint runtime)
{
    notifyF->show();
    QPalette teste;
    switch(status) {
        case Exit::Success :
	    notifyL->setText("Search finished in " + KGlobal::locale()->formatDuration(runtime) );
            teste.setColor( QPalette::Normal, QPalette::Window, QColor(0,255,0,150));
            notifyL->setPalette(teste);
            notifyL->setAutoFillBackground(true);
            m_notifyT.start(100);
	    break;
	case Exit::Failed :
	    notifyL->setText("Search Failed " + KGlobal::locale()->formatDuration(runtime) );
            teste.setColor(QPalette::Normal, QPalette::Window, QColor(255,0,0,150));
            notifyL->setPalette(teste);
            notifyL->setAutoFillBackground(true);
            m_notifyT.start(50);
	    break;
	case Exit::Quit : break;
	case Exit::Kill : break;
	case Exit::Unknown : break;
    }
}

void PkAddRm::notifyUpdate()
{
    QPalette palleteN(notifyL->palette());
    QColor colorN(palleteN.color(QPalette::Normal, QPalette::Window));
    if ( colorN.alpha() <= 0 ) {
        m_notifyT.stop();
        notifyL->setAutoFillBackground(false);
	if ( !actionPB->isVisible() )
	    notifyF->hide();
    }
    else {
        colorN.setAlpha(colorN.alpha() - 5);
        palleteN.setColor(QPalette::Normal, QPalette::Window, colorN);
        notifyL->setPalette(palleteN);
    }
}

void PkAddRm::Description(Package *p, const QString& license, const QString& group, const QString& detail, const QString& url, qulonglong size)
{
    qDebug() << p->id();
    //ask required by packages
    m_pkClient_req->getRequires("none", p, false);

    //ask depends on packages
    m_pkClient_dep->getDepends("none", p, false);

    //ask files in packages
    m_pkClient_files->getFiles(p);

    //format and show description
    QString description;
    description += "<b>" + i18n("Package Name") + ":</b> " + p->name() + "<br />";
    if ( !license.isEmpty() && license != "unknown" )
        description += "<b>" + i18n("License") + ":</b> " + license + "<br />";
    if ( !group.isEmpty() && group != "unknown" )
        description += "<b>" + i18n("Group") + ":</b> " + group + "<br />";
    if ( !detail.isEmpty() )
        description += "<b>" + i18n("Details") + ":</b> " + detail + "<br />";
    if ( !url.isEmpty() )
        description += "<b>" + i18n("Home Page") + ":</b> <a href=\"" + url + "\">" + url + "</a><br />";
    if ( size > 0 )
        description += "<b>" + i18n("Size") + ":</b> " + KGlobal::locale()->formatByteSize(size);
    descriptionKTB->setHtml(description);
    infoShow();
}

void PkAddRm::Files(Package *, QStringList files)
{
    filesPTE->clear();
    for (int i = 0; i < files.size(); ++i)
        filesPTE->appendPlainText(files.at(i));
    infoShow();
}

void PkAddRm::FilterMenu(const QStringList &filters)
{
    if(filters.size() != 0) {
        m_toolQM = new QMenu(this);
        filtersTB->setMenu(m_toolQM);

        if(filters.contains("installed")) {
            // Installed
            QMenu *menuInstalled = new QMenu(i18n("Installed"), m_toolQM);
            m_toolQM->addMenu(menuInstalled);
            QActionGroup *installedGroup = new QActionGroup(menuInstalled);
            installedGroup->setExclusive(true);
            QAction *installedTrue = new QAction(i18n("installed"), installedGroup);
            installedTrue->setCheckable(true);
            installedTrue->setData("installed");
            installedGroup->addAction(installedTrue);
            menuInstalled->addAction(installedTrue);

            QAction *installedFalse = new QAction(i18n("~installed"), installedGroup);
            installedFalse->setCheckable(true);
            installedFalse->setData("~installed");
            installedGroup->addAction(installedFalse);
            menuInstalled->addAction(installedFalse);

            QAction *installedNone = new QAction(i18n("No filter"), installedGroup);
            installedNone->setCheckable(true);
            installedNone->setChecked(true);
            installedGroup->addAction(installedNone);
            menuInstalled->addAction(installedNone);

            actions << installedTrue << installedFalse << installedNone;
        }
        if(filters.contains("devel")) {
            // Development
            QMenu *menuDevelopment = new QMenu(i18n("Development"), m_toolQM);
            m_toolQM->addMenu(menuDevelopment);
            QActionGroup *developmentGroup = new QActionGroup(menuDevelopment);
            developmentGroup->setExclusive(true);

            QAction *developmentTrue = new QAction(i18n("development"), developmentGroup);
            developmentTrue->setCheckable(true);
            developmentTrue->setData("development");
            developmentGroup->addAction(developmentTrue);
            menuDevelopment->addAction(developmentTrue);

            QAction *developmentFalse = new QAction(i18n("~development"), developmentGroup);
            developmentFalse->setCheckable(true);
            developmentFalse->setData("~development");
            developmentGroup->addAction(developmentFalse);
            menuDevelopment->addAction(developmentFalse);

            QAction *developmentNone = new QAction(i18n("No filter"), developmentGroup);
            developmentNone->setCheckable(true);
            developmentNone->setChecked(true);
            developmentGroup->addAction(developmentNone);
            menuDevelopment->addAction(developmentNone);

            actions << developmentTrue << developmentFalse << developmentNone;
        }
        if(filters.contains("gui")) {
            // Graphical
            QMenu *menuGui = new QMenu(i18n("Graphical"), m_toolQM);
            m_toolQM->addMenu(menuGui);
            QActionGroup *guiGroup = new QActionGroup(menuGui);
            guiGroup->setExclusive(true);

            QAction *guiTrue = new QAction(i18n("gui"), guiGroup);
            guiTrue->setCheckable(true);
            guiTrue->setData("gui");
            guiGroup->addAction(guiTrue);
            menuGui->addAction(guiTrue);

            QAction *guiFalse = new QAction(i18n("~gui"), guiGroup);
            guiFalse->setCheckable(true);
            guiFalse->setData("~gui");
            guiGroup->addAction(guiFalse);
            menuGui->addAction(guiFalse);

            QAction *guiNone = new QAction(i18n("No filter"), guiGroup);
            guiNone->setCheckable(true);
            guiNone->setChecked(true);
            guiGroup->addAction(guiNone);
            menuGui->addAction(guiNone);

            actions << guiTrue << guiFalse << guiNone;
        }
        if(filters.contains("free")) {
            // Free
            QMenu *menuFree = new QMenu(i18n("Free"), m_toolQM);
            m_toolQM->addMenu(menuFree);
            QActionGroup *freeGroup = new QActionGroup(menuFree);
            freeGroup->setExclusive(true);

            QAction *freeTrue = new QAction(i18n("free"), freeGroup);
            freeTrue->setCheckable(true);
            freeTrue->setData("free");
            freeGroup->addAction(freeTrue);
            menuFree->addAction(freeTrue);

            QAction *freeFalse = new QAction(i18n("~free"), freeGroup);
            freeFalse->setCheckable(true);
            freeFalse->setData("~free");
            freeGroup->addAction(freeFalse);
            menuFree->addAction(freeFalse);

            QAction *freeNone = new QAction(i18n("No filter"), freeGroup);
            freeNone->setCheckable(true);
            freeNone->setChecked(true);
            freeGroup->addAction(freeNone);
            menuFree->addAction(freeNone);

            actions << freeTrue << freeFalse << freeNone;
        }
        if(filters.contains("basename")) {
            m_toolQM->addSeparator();
            QAction *basename = new QAction(i18n("Hide subpackages"), m_toolQM);
            basename->setCheckable(true);
            basename->setData("basename");
            m_toolQM->addAction(basename);

            actions << basename;
        }

    }
    else {
        filtersTB->setDisabled(true);
    }
}

QString PkAddRm::filters()
{
    QStringList buffer;
    for(int i = 0 ; i < actions.size() ; ++i) {
        if(actions.at(i)->isChecked())
            if(actions.at(i)->data() != QVariant())
                buffer << actions.at(i)->data().toString();
    }
    if(buffer.size() == 0) buffer << "none";
        return buffer.join(";");
}

#include "PkAddRm.moc"