• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.4 API Reference
  • KDE Home
  • Contact Us
 

KNewStuff

  • knewstuff
  • knewstuff3
downloadwidget.cpp
Go to the documentation of this file.
1 /*
2  knewstuff3/ui/downloaddialog.cpp.
3  Copyright (C) 2005 by Enrico Ros <eros.kde@email.it>
4  Copyright (C) 2005 - 2007 Josef Spillner <spillner@kde.org>
5  Copyright (C) 2007 Dirk Mueller <mueller@kde.org>
6  Copyright (C) 2007-2009 Jeremy Whiting <jpwhiting@kde.org>
7  Copyright (C) 2009-2010 Frederik Gladhorn <gladhorn@kde.org>
8  Copyright (C) 2010 Reza Fatahilah Shah <rshah0385@kireihana.com>
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Lesser General Public
12  License as published by the Free Software Foundation; either
13  version 2.1 of the License, or (at your option) any later version.
14 
15  This library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public
21  License along with this library. If not, see <http://www.gnu.org/licenses/>.
22 */
23 
24 #include "downloadwidget.h"
25 #include "downloadwidget_p.h"
26 
27 #include <QtCore/QTimer>
28 #include <QtGui/QScrollBar>
29 #include <QtGui/QKeyEvent>
30 
31 #include <kmessagebox.h>
32 #include <kcomponentdata.h>
33 #include <kaboutdata.h>
34 #include <kdebug.h>
35 
36 #include "ui/itemsmodel.h"
37 #include "ui/itemsviewdelegate.h"
38 #include "ui/itemsgridviewdelegate.h"
39 
40 using namespace KNS3;
41 
42 DownloadWidget::DownloadWidget(QWidget* parent)
43  : QWidget(parent)
44  , d(new DownloadWidgetPrivate(this))
45 {
46  KComponentData component = KGlobal::activeComponent();
47  QString name = component.componentName();
48  init(name + ".knsrc");
49 }
50 
51 DownloadWidget::DownloadWidget(const QString& configFile, QWidget * parent)
52  : QWidget(parent)
53  , d(new DownloadWidgetPrivate(this))
54 {
55  init(configFile);
56 }
57 
58 void DownloadWidget::init(const QString& configFile)
59 {
60  d->init(configFile);
61 }
62 
63 DownloadWidget::~DownloadWidget()
64 {
65  delete d;
66 }
67 
68 Entry::List DownloadWidget::changedEntries()
69 {
70  Entry::List entries;
71  foreach (const EntryInternal &e, d->changedEntries) {
72  entries.append(e.toEntry());
73  }
74  return entries;
75 }
76 
77 Entry::List DownloadWidget::installedEntries()
78 {
79  Entry::List entries;
80  foreach (const EntryInternal &e, d->changedEntries) {
81  if (e.status() == Entry::Installed) {
82  entries.append(e.toEntry());
83  }
84  }
85  return entries;
86 }
87 
88 
89 DownloadWidgetPrivate::DownloadWidgetPrivate(DownloadWidget* q)
90 : q(q)
91 , engine(new Engine)
92 , model(new ItemsModel(engine))
93 , messageTimer(0)
94 , dialogMode(false)
95 {
96 }
97 
98 DownloadWidgetPrivate::~DownloadWidgetPrivate()
99 {
100  delete messageTimer;
101  delete delegate;
102  delete model;
103  delete engine;
104 }
105 
106 void DownloadWidgetPrivate::slotResetMessage() // SLOT
107 {
108  ui.m_titleWidget->setComment(QString());
109 }
110 
111 void DownloadWidgetPrivate::slotNetworkTimeout() // SLOT
112 {
113  displayMessage(i18n("Timeout. Check Internet connection."), KTitleWidget::ErrorMessage);
114 }
115 
116 void DownloadWidgetPrivate::sortingChanged()
117 {
118  Provider::SortMode sortMode = Provider::Newest;
119  if (ui.ratingRadio->isChecked()) {
120  sortMode = Provider::Rating;
121  } else if (ui.mostDownloadsRadio->isChecked()) {
122  sortMode = Provider::Downloads;
123  } else if (ui.installedRadio->isChecked()) {
124  sortMode = Provider::Installed;
125  }
126 
127  model->clearEntries();
128  if (sortMode == Provider::Installed) {
129  ui.m_searchEdit->clear();
130  }
131  ui.m_searchEdit->setEnabled(sortMode != Provider::Installed);
132 
133  engine->setSortMode(sortMode);
134 }
135 
136 void DownloadWidgetPrivate::slotUpdateSearch()
137 {
138  if (searchTerm == ui.m_searchEdit->text().trimmed()) {
139  return;
140  }
141  searchTerm = ui.m_searchEdit->text().trimmed();
142 }
143 
144 void DownloadWidgetPrivate::slotSearchTextChanged()
145 {
146  if (searchTerm == ui.m_searchEdit->text().trimmed()) {
147  return;
148  }
149  searchTerm = ui.m_searchEdit->text().trimmed();
150  engine->setSearchTerm(ui.m_searchEdit->text().trimmed());
151 }
152 
153 void DownloadWidgetPrivate::slotCategoryChanged(int idx)
154 {
155  if (idx == 0) {
156  // All Categories item selected, reset filter
157  engine->setCategoriesFilter(QStringList());
158 
159  } else {
160  QString category = ui.m_categoryCombo->currentText();
161  if (!category.isEmpty()) {
162  QStringList filter(category);
163  engine->setCategoriesFilter(filter);
164  }
165  }
166 }
167 
168 void DownloadWidgetPrivate::slotInfo(QString provider, QString server, QString version)
169 {
170  QString link = QString("<a href=\"%1\">%1</a>").arg(server);
171  QString infostring = i18n("Server: %1", link);
172  infostring += i18n("<br />Provider: %1", provider);
173  infostring += i18n("<br />Version: %1", version);
174 
175  KMessageBox::information(0,
176  infostring,
177  i18n("Provider information"));
178 }
179 
180 void DownloadWidgetPrivate::slotEntryChanged(const EntryInternal& entry)
181 {
182  changedEntries.insert(entry);
183  model->slotEntryChanged(entry);
184 }
185 
186 void DownloadWidgetPrivate::slotPayloadFailed(const EntryInternal& entry)
187 {
188  KMessageBox::error(0, i18n("Could not install %1", entry.name()),
189  i18n("Get Hot New Stuff!"));
190 }
191 
192 void DownloadWidgetPrivate::slotPayloadLoaded(KUrl url)
193 {
194  Q_UNUSED(url)
195 }
196 
197 void DownloadWidgetPrivate::slotError(const QString& message)
198 {
199  KMessageBox::error(0, message, i18n("Get Hot New Stuff"));
200 }
201 
202 void DownloadWidgetPrivate::scrollbarValueChanged(int value)
203 {
204  if ((double)value/ui.m_listView->verticalScrollBar()->maximum() > 0.9) {
205  engine->requestMoreData();
206  }
207 }
208 
209 void DownloadWidgetPrivate::init(const QString& configFile)
210 {
211  m_configFile = configFile;
212  ui.setupUi(q);
213  ui.m_titleWidget->setVisible(false);
214  ui.closeButton->setVisible(dialogMode);
215  ui.backButton->setVisible(false);
216  ui.backButton->setGuiItem(KStandardGuiItem::Back);
217  q->connect(ui.backButton, SIGNAL(clicked()), q, SLOT(slotShowOverview()));
218 
219  q->connect(engine, SIGNAL(signalBusy(QString)), ui.progressIndicator, SLOT(busy(QString)));
220  q->connect(engine, SIGNAL(signalError(QString)), ui.progressIndicator, SLOT(error(QString)));
221  q->connect(engine, SIGNAL(signalIdle(QString)), ui.progressIndicator, SLOT(idle(QString)));
222 
223  q->connect(engine, SIGNAL(signalProvidersLoaded()), q, SLOT(slotProvidersLoaded()));
224  // Entries have been fetched and should be shown:
225  q->connect(engine, SIGNAL(signalEntriesLoaded(KNS3::EntryInternal::List)), q, SLOT(slotEntriesLoaded(KNS3::EntryInternal::List)));
226 
227  // An entry has changes - eg because it was installed
228  q->connect(engine, SIGNAL(signalEntryChanged(KNS3::EntryInternal)), q, SLOT(slotEntryChanged(KNS3::EntryInternal)));
229 
230  q->connect(engine, SIGNAL(signalResetView()), model, SLOT(clearEntries()));
231  q->connect(engine, SIGNAL(signalEntryPreviewLoaded(KNS3::EntryInternal,KNS3::EntryInternal::PreviewType)),
232  model, SLOT(slotEntryPreviewLoaded(KNS3::EntryInternal,KNS3::EntryInternal::PreviewType)));
233 
234  engine->init(configFile);
235 
236  delegate = new ItemsViewDelegate(ui.m_listView, engine, q);
237  ui.m_listView->setItemDelegate(delegate);
238  ui.m_listView->setModel(model);
239 
240  ui.iconViewButton->setIcon(KIcon("view-list-icons"));
241  ui.iconViewButton->setToolTip(i18n("Icons view mode"));
242  ui.listViewButton->setIcon(KIcon("view-list-details"));
243  ui.listViewButton->setToolTip(i18n("Details view mode"));
244 
245  q->connect(ui.listViewButton, SIGNAL(clicked()), q, SLOT(slotListViewListMode()));
246  q->connect(ui.iconViewButton, SIGNAL(clicked()), q, SLOT(slotListViewIconMode()));
247 
248  q->connect(ui.newestRadio, SIGNAL(clicked()), q, SLOT(sortingChanged()));
249  q->connect(ui.ratingRadio, SIGNAL(clicked()), q, SLOT(sortingChanged()));
250  q->connect(ui.mostDownloadsRadio, SIGNAL(clicked()), q, SLOT(sortingChanged()));
251  q->connect(ui.installedRadio, SIGNAL(clicked()), q, SLOT(sortingChanged()));
252 
253  q->connect(ui.m_searchEdit, SIGNAL(textChanged(QString)), q, SLOT(slotSearchTextChanged()));
254  q->connect(ui.m_searchEdit, SIGNAL(editingFinished()), q, SLOT(slotUpdateSearch()));
255 
256  ui.m_providerLabel->setVisible(false);
257  ui.m_providerCombo->setVisible(false);
258  ui.m_providerCombo->addItem(i18n("All Providers"));
259 
260  QStringList categories = engine->categories();
261  if (categories.size() < 2) {
262  ui.m_categoryLabel->setVisible(false);
263  ui.m_categoryCombo->setVisible(false);
264  } else {
265  ui.m_categoryCombo->addItem(i18n("All Categories"));
266  foreach(const QString& category, categories) {
267  ui.m_categoryCombo->addItem(category);
268  }
269  }
270 
271  ui.detailsStack->widget(0)->layout()->setMargin(0);
272  ui.detailsStack->widget(1)->layout()->setMargin(0);
273 
274  q->connect(ui.m_categoryCombo, SIGNAL(activated(int)), q, SLOT(slotCategoryChanged(int)));
275 
276  // let the search line edit trap the enter key, otherwise it closes the dialog
277  ui.m_searchEdit->setTrapReturnKey(true);
278 
279  q->connect(ui.m_listView->verticalScrollBar(), SIGNAL(valueChanged(int)), q, SLOT(scrollbarValueChanged(int)));
280  q->connect(ui.m_listView, SIGNAL(doubleClicked(QModelIndex)), delegate, SLOT(slotDetailsClicked(QModelIndex)));
281 
282  details = new EntryDetails(engine, &ui);
283  q->connect(delegate, SIGNAL(signalShowDetails(KNS3::EntryInternal)), q, SLOT(slotShowDetails(KNS3::EntryInternal)));
284 
285  slotShowOverview();
286 }
287 
288 void DownloadWidgetPrivate::slotListViewListMode()
289 {
290  ui.listViewButton->setChecked(true);
291  ui.iconViewButton->setChecked(false);
292  setListViewMode(QListView::ListMode);
293 }
294 
295 void DownloadWidgetPrivate::slotListViewIconMode()
296 {
297  ui.listViewButton->setChecked(false);
298  ui.iconViewButton->setChecked(true);
299  setListViewMode(QListView::IconMode);
300 }
301 
302 void DownloadWidgetPrivate::setListViewMode(QListView::ViewMode mode)
303 {
304  if (ui.m_listView->viewMode() == mode) {
305  return;
306  }
307 
308  ItemsViewBaseDelegate* oldDelegate = delegate;
309  if (mode == QListView::ListMode) {
310  delegate = new ItemsViewDelegate(ui.m_listView, engine, q);
311  ui.m_listView->setViewMode(QListView::ListMode);
312  ui.m_listView->setResizeMode(QListView::Fixed);
313  } else {
314  delegate = new ItemsGridViewDelegate(ui.m_listView, engine, q);
315  ui.m_listView->setViewMode(QListView::IconMode);
316  ui.m_listView->setResizeMode(QListView::Adjust);
317  }
318  ui.m_listView->setItemDelegate(delegate);
319  delete oldDelegate;
320 
321  q->connect(ui.m_listView, SIGNAL(doubleClicked(QModelIndex)), delegate, SLOT(slotDetailsClicked(QModelIndex)));
322  q->connect(delegate, SIGNAL(signalShowDetails(KNS3::EntryInternal)), q, SLOT(slotShowDetails(KNS3::EntryInternal)));
323 }
324 
325 void DownloadWidgetPrivate::slotProvidersLoaded()
326 {
327  kDebug() << "providers loaded";
328  engine->reloadEntries();
329 }
330 
331 void DownloadWidgetPrivate::slotEntriesLoaded(const EntryInternal::List& entries)
332 {
333  foreach(const KNS3::EntryInternal &entry, entries) {
334  if (!categories.contains(entry.category())) {
335  kDebug() << "Found category: " << entry.category();
336  categories.insert(entry.category());
337  }
338  }
339  model->slotEntriesLoaded(entries);
340 }
341 
342 void DownloadWidgetPrivate::displayMessage(const QString & msg, KTitleWidget::MessageType type, int timeOutMs)
343 {
344  if (!messageTimer) {
345  messageTimer = new QTimer;
346  messageTimer->setSingleShot(true);
347  q->connect(messageTimer, SIGNAL(timeout()), q, SLOT(slotResetMessage()));
348  }
349  // stop the pending timer if present
350  messageTimer->stop();
351 
352  // set text to messageLabel
353  ui.m_titleWidget->setComment(msg, type);
354 
355  // single shot the resetColors timer (and create it if null)
356  if (timeOutMs > 0) {
357  //kDebug(551) << "starting the message timer for " << timeOutMs;
358  messageTimer->start(timeOutMs);
359  }
360 }
361 
362 void DownloadWidgetPrivate::slotShowDetails(const KNS3::EntryInternal& entry)
363 {
364  if (!entry.isValid()) {
365  kDebug() << "invalid entry";
366  return;
367  }
368  titleText = ui.m_titleWidget->text();
369 
370  ui.backButton->setVisible(true);
371  ui.detailsStack->setCurrentIndex(1);
372  ui.descriptionScrollArea->verticalScrollBar()->setValue(0);
373  ui.preview1->setImage(QImage());
374  ui.preview2->setImage(QImage());
375  ui.preview3->setImage(QImage());
376  ui.previewBig->setImage(QImage());
377  details->setEntry(entry);
378 }
379 
380 void DownloadWidgetPrivate::slotShowOverview()
381 {
382  ui.backButton->setVisible(false);
383 
384  ui.updateButton->setVisible(false);
385  ui.installButton->setVisible(false);
386  ui.becomeFanButton->setVisible(false);
387  ui.uninstallButton->setVisible(false);
388 
389  ui.detailsStack->setCurrentIndex(0);
390  ui.m_titleWidget->setText(titleText);
391 }
392 
393 
394 #include "downloadwidget.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jun 1 2013 22:04:45 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KNewStuff

Skip menu "KNewStuff"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.10.4 API Reference

Skip menu "kdelibs-4.10.4 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal