summaryrefslogtreecommitdiff
path: root/gui/KPackageKit/PkNotify.cpp
blob: ffb3a9e18880e3043f94ba2359f5e2dd07675ad9 (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
/***************************************************************************
 *   Copyright (C) 2008 by Daniel Nicoletti   *
 *   dantti85-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 "PkNotify.h"
#include <KMessageBox>
#include <KLocale>
#include <KIcon>
#include <QSize>
#include <KConfig>
#include <KConfigGroup>

#include <KDebug>

namespace kpackagekit {

PkNotify::PkNotify( QObject *parent ) : QObject( parent )
{
//     descriptionKTB->hide();

    //initialize the model, delegate, client and  connect it's signals
//     treeView->setModel(m_pkg_model_updates = new PkAddRmModel(this));
//     treeView->setItemDelegate(pkg_delegate = new PkAddRmDelegate(this));

    // Create a new daemon
    m_daemon = new Daemon(this);

    m_pkClient_updates = m_daemon->newTransaction();
//     connect( m_pkClient_updates, SIGNAL( GotPackage(Package *) ), m_pkg_model_updates, SLOT( addPackage(Package *) ) );
//     connect( m_pkClient_updates, SIGNAL(Finished(Exit::Value, uint)), this, SLOT(Finished(Exit::Value, uint)) );
//     connect( m_pkClient_updates, SIGNAL(Files(Package *, QStringList)), this, SLOT(Files(Package *, QStringList)) );
//     connect( m_pkClient_updates, SIGNAL( Message(const QString&, const QString&) ), this, SLOT( Message(const QString&, const QString&) ) );
//     connect( m_pkClient_updates, SIGNAL( ErrorCode(const QString&, const QString&) ), this, SLOT( Message(const QString&, const QString&) ) );
//     m_pkClient_updates->getUpdates("none");
}

PkNotify::~PkNotify()
{
}

void PkNotify::displayUpdates()
{
    connect( m_pkClient_updates, SIGNAL( GotPackage(Package *) ), this, SLOT( GotPackage(Package *) ) );
    connect( m_pkClient_updates, SIGNAL( Finished(Exit::Value, uint) ), this, SLOT( UpdatesFinished(Exit::Value, uint)) );
    m_pkClient_updates->getUpdates("none");
}

void PkNotify::finishInstallingRebootReq()
{
}

void PkNotify::updatesBeingInstalled()
{
}

void PkNotify::GotPackage(Package *p)
{
    packages << p;
}

void PkNotify::UpdatesFinished(Exit::Value status, uint /*runtime*/)
{
    qDebug() << "trans getUpdates finished: " << status ;
    if (status == Exit::Success) {
	if ( packages.size() ) {
	    kDebug() << "We have updates let's show...";
	    updatesNotification = new KNotification( "ShowUpdates" );
	    // here we check if they are too many
	    // i think 5 is the maximun number to show them detailed
// 	    if ( packages.size() <= 5 ) {
// 	        QString text;
// 		text.append( i18np("<b>You have one update:", "<br>You have %1 updates:</b>", packages.size() ) );
// 		for ( int i = 0; i < packages.size(); i++) {
// 		    text.append( "<br><b>" + packages.at(i)->name() + "</b> - " + packages.at(i)->summary() );
// 		}
// 		updatesNotification->setText(text);
// 	    }
// 	    else {
	        QString text;
		text.append( i18n("<b>You have %1 updates:</b>", packages.size() ) );
		
		QHash<QString, int> info;
		
// 		
    
		/*for ( int i = 0; i < packages.size(); i++) {
		    if ( p->info() == "available" || p->info() == "installed" )
			if ( p->data() == "debian" )
			    return m_iconDeb;
			else if ( p->data() == "fedora" )
			    return m_iconRpm;
			else
			    return m_iconGeneric;
		    else if ( p->info() == "bugfix" )
			return m_iconBugFix;
		    else if ( p->info() == "security-important" )
			return m_iconImportant;
		    else if ( p->info() == "security-low" )
			return m_iconLow;
		    else if ( p->info() == "enhancement" )
			return m_iconEnhancement;
		    else if ( p->info() == "security" )
			return m_iconSecurity;
		    else if ( p->info() == "normal" )
			return m_iconNormal;
		    else
			 return m_iconGeneric;
			 
		*/    
		
		
		for ( int i = 0; i < packages.size(); i++) {
		    info[ packages.at(i)->info() ] = ++info[ packages.at(i)->info() ];
		}
		// if we have only one kind of update don't show total of updates
		if ( info ) {
		    text.append( i18n("<b>You have %1 %1 updates:</b>", packages.size() ) );
		}
		else {
		    QHashIterator<QString, int> i(hash);
		    while (i.hasNext()) {
			i.next();
			cout << i.key() << ": " << i.value() << endl;
		    }
		}
		updatesNotification->setText(text);
		kDebug() << info;
// 	    }
	    KIcon icon("security-high");
	    updatesNotification->setPixmap( icon.pixmap( QSize(128,128) ) );
	    QStringList actions( i18n("Update Now") );
	    actions << i18n("Not Now");
	    actions << i18n("Don't ask anymore");
	    updatesNotification->setActions(actions);
	    //TODO close the notifications !! memoryleak
	    updatesNotification->setFlags( KNotification::Persistent );
	    connect( updatesNotification, SIGNAL( activated(uint) ), this , SLOT( updatesActions(uint) ) );
            updatesNotification->sendEvent();
	}
    }
}

void PkNotify::updatesActions(uint action)
{
    updatesNotification->close();
    kDebug() << "update Action " << action;
    switch (action) {
	case 1:
	    break;
	case 2:
	    break;
	case 3:
	    KConfig config("KPackageKit");
	    KConfigGroup smartIconGroup( &config, "SmartIcon" );
	    smartIconGroup.writeEntry( "notifyUpdates", 0 );
            break;
    }
}


}

#include "PkNotify.moc"