1/*
2 Copyright (c) 2009 Volker Krause <vkrause@kde.org>
3 Copyright (c) 2010 Tom Albers <toma@kde.org>
4
5 This library is free software; you can redistribute it and/or modify it
6 under the terms of the GNU Library General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or (at your
8 option) any later version.
9
10 This library is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301, USA.
19*/
20
21#include "providerpage.h"
22#include "global.h"
23
24#include <KDebug>
25#include <QSortFilterProxyModel>
26#include <knewstuff3/downloadmanager.h>
27
28ProviderPage::ProviderPage(KAssistantDialog* parent) :
29 Page( parent ),
30 m_model( new QStandardItemModel( this ) ),
31 m_downloadManager( new KNS3::DownloadManager( this ) ),
32 m_newPageWanted( false ),
33 m_newPageReady( false )
34{
35 ui.setupUi( this );
36
37 QSortFilterProxyModel *proxy = new QSortFilterProxyModel( this );
38 proxy->setSourceModel( m_model );
39 ui.listView->setModel( proxy );
40 ui.searchLine->setProxy( proxy );
41
42 m_fetchItem = new QStandardItem( i18n( "Fetching provider list..." ) );
43 m_model->appendRow( m_fetchItem );
44
45 // we can start the search, whenever the user reaches this page, chances
46 // are we have the full list.
47 connect( m_downloadManager, SIGNAL(searchResult(KNS3::Entry::List)),
48 SLOT(fillModel(KNS3::Entry::List)) );
49 connect( m_downloadManager, SIGNAL(entryStatusChanged(KNS3::Entry)),
50 SLOT(providerStatusChanged(KNS3::Entry)) );
51 m_downloadManager->setSearchOrder( KNS3::DownloadManager::Alphabetical );
52
53 connect( ui.listView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), SLOT(selectionChanged()) );
54
55 kDebug();
56}
57
58void ProviderPage::startFetchingData()
59{
60 m_downloadManager->search( 0, 100000 );
61}
62
63void ProviderPage::fillModel( const KNS3::Entry::List& list )
64{
65 kDebug();
66 m_model->removeRows( m_model->indexFromItem( m_fetchItem ).row(), 1 );
67
68 // KNS3::Entry::Entry() is private, so we need to save the whole list.
69 // we can not use a QHash or whatever, as that needs that constructor...
70 m_providerEntries = list;
71
72 foreach ( const KNS3::Entry& e, list ) {
73 kDebug() << "Found Entry: " << e.name();
74
75 QStandardItem *item = new QStandardItem( e.name() );
76 item->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
77 item->setData( e.name(), Qt::ToolTipRole );
78 item->setData( e.id(), Qt::UserRole );
79 item->setData( e.providerId(), Qt::UserRole+ 1 );
80 m_model->appendRow( item );
81 }
82}
83
84void ProviderPage::selectionChanged()
85{
86 if ( ui.listView->selectionModel()->hasSelection() ) {
87 setValid( true );
88 } else {
89 setValid( false );
90 }
91}
92
93void ProviderPage::leavePageNext()
94{
95 m_newPageReady = false;
96 if ( !ui.listView->selectionModel()->hasSelection() )
97 return;
98 const QModelIndex index = ui.listView->selectionModel()->selectedIndexes().first();
99 if ( !index.isValid() )
100 return;
101
102 const QSortFilterProxyModel *proxy = static_cast<const QSortFilterProxyModel*>( ui.listView->model() );
103 const QStandardItem* item = m_model->itemFromIndex( proxy->mapToSource( index ) );
104 kDebug() << "Item selected:"<< item->text();
105
106 // download and execute it...
107 foreach ( const KNS3::Entry& e, m_providerEntries ) {
108 if ( e.id() == item->data( Qt::UserRole ) &&
109 e.providerId() == item->data( Qt::UserRole+ 1 ) ) {
110
111 m_wantedProvider.entryId = e.id();
112 m_wantedProvider.entryProviderId = e.providerId();
113
114 if ( e.status() == KNS3::Entry::Installed ) {
115 kDebug() << "already installed" << e.installedFiles();
116 findDesktopAndSetAssistant( e.installedFiles() );
117 } else {
118 kDebug() << "Starting download for " << e.name();
119 m_downloadManager->installEntry( e );
120 }
121
122 break;
123 }
124 }
125}
126
127void ProviderPage::providerStatusChanged( const KNS3::Entry& e )
128{
129 kDebug() << e.name();
130 if ( e.id() == m_wantedProvider.entryId &&
131 e.providerId() == m_wantedProvider.entryProviderId &&
132 e.status() == KNS3::Entry::Installed ) {
133 findDesktopAndSetAssistant( e.installedFiles() );
134 }
135}
136
137void ProviderPage::findDesktopAndSetAssistant( const QStringList& list )
138{
139 foreach ( const QString& file, list ) {
140 kDebug() << file;
141 if ( file.endsWith( QLatin1String ( ".desktop" ) ) ) {
142 kDebug() << "Yay, a desktop file!" << file;
143 Global::setAssistant( file );
144 m_newPageReady = true;
145 if ( m_newPageWanted ) {
146 kDebug() << "New page was already requested, now we are done, approve it";
147 emit leavePageNextOk();
148 }
149 break;
150 }
151 }
152}
153
154QTreeView *ProviderPage::treeview() const
155{
156 return ui.listView;
157}
158
159void ProviderPage::leavePageBackRequested()
160{
161 emit leavePageBackOk();
162 emit ghnsNotWanted();
163}
164
165void ProviderPage::leavePageNextRequested()
166{
167 m_newPageWanted = true;
168 if ( m_newPageReady ) {
169 kDebug() << "New page requested and we are done, so ok...";
170 emit leavePageNextOk();
171 } else {
172 kDebug() << "New page requested, but we are not done yet...";
173 }
174}
175
176