• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kget

  • sources
  • kde-4.12
  • kdenetwork
  • kget
  • ui
  • metalinkcreator
metalinkcreator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 * Copyright (C) 2009 Matthias Fuchs <mat69@gmx.net> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18 ***************************************************************************/
19 
20 #include "metalinkcreator.h"
21 #include "filedlg.h"
22 #include "dragdlg.h"
23 #include "localemodels.h"
24 #include "generalwidget.h"
25 
26 #include <QtCore/QTimer>
27 #include <QtGui/QDragEnterEvent>
28 #include <QtGui/QLabel>
29 #include <QtGui/QSortFilterProxyModel>
30 #include <QtGui/QStandardItemModel>
31 
32 #include <KFileDialog>
33 #include <KLocale>
34 #include <KMessageBox>
35 #include <KPushButton>
36 #include <KStandardDirs>
37 
38 //TODO for 4.4 look at the changes of the newest Draft --> what elements have to be added/removed
39 
40 FileWidget::FileWidget(QWidget *parent)
41  : QWidget(parent)
42 {
43  setAcceptDrops(true);
44 }
45 
46 
47 void FileWidget::dragEnterEvent(QDragEnterEvent *event)
48 {
49  if (event->mimeData()->hasUrls())
50  {
51  event->acceptProposedAction();
52  }
53 }
54 
55 void FileWidget::dropEvent(QDropEvent *event)
56 {
57  QList<QUrl> urls = event->mimeData()->urls();
58 
59  event->acceptProposedAction();
60 
61  QList<KUrl> kurls;
62  foreach (const QUrl &url, urls) {
63  kurls.append(url);
64  }
65 
66  if (!kurls.isEmpty()) {
67  emit urlsDropped(kurls);
68  }
69 }
70 
71 MetalinkCreator::MetalinkCreator(QWidget *parent)
72  : KAssistantDialog(parent),
73  m_needUrlCount(0),
74  m_countrySort(0),
75  m_languageModel(0),
76  m_languageSort(0),
77  m_introduction(0),
78  m_generalPage(0),
79  m_filesModel(0)
80 {
81  create();
82 
83  connect(this, SIGNAL(user1Clicked()), this, SLOT(slotSave()));
84  connect(this, SIGNAL(currentPageChanged(KPageWidgetItem*,KPageWidgetItem*)), this, SLOT(slotUpdateAssistantButtons(KPageWidgetItem*,KPageWidgetItem*)));
85 
86  qRegisterMetaType<KGetMetalink::File>("KGetMetalink::File");
87  connect(&m_thread, SIGNAL(fileResult(KGetMetalink::File)), this, SLOT(slotAddFile(KGetMetalink::File)));
88  connect(&m_thread, SIGNAL(finished()), this, SLOT(slotThreadFinished()));
89 
90  setCaption(i18n("Create a Metalink"));
91  showButton(KDialog::Help, false);
92 }
93 
94 MetalinkCreator::~MetalinkCreator()
95 {
96 }
97 
98 void MetalinkCreator::slotUpdateAssistantButtons(KPageWidgetItem *to, KPageWidgetItem *from)
99 {
100  //once we leave the introduction page the data is being loaded
101  if (m_introduction && m_generalPage && (from == m_introduction) && (to == m_generalPage))
102  {
103  load();
104  }
105 
106  //it is impossible to return to the introduction page
107  if (to == m_generalPage)
108  {
109  enableButton(KDialog::User3, false);
110  }
111  else
112  {
113  enableButton(KDialog::User3, true);
114  }
115 
116  if (!m_filesModel->rowCount()) {
117  uiFiles.infoWidget->setText(i18n("Add at least one file."));
118  } else if (m_needUrlCount) {
119  uiFiles.infoWidget->setText(i18n("You need to set mirrors for the entries with an icon."));
120  }
121  uiFiles.infoWidget->setVisible(!m_filesModel->rowCount() || m_needUrlCount);
122 
123  //only enable finish then the metalink is valid (i.e. no required data missing)
124  //and the thread is not running
125  enableButton(KDialog::User1, metalink.isValid() && !m_thread.isRunning());
126 }
127 
128 void MetalinkCreator::create()
129 {
130  createIntroduction();
131  m_general = new GeneralWidget(this);
132  m_generalPage = addPage(m_general, i18n("General optional information for the metalink."));
133  QTimer::singleShot(0, this, SLOT(slotDelayedCreation()));
134 }
135 
136 void MetalinkCreator::slotDelayedCreation()
137 {
138  CountryModel *countryModel = new CountryModel(this);
139  countryModel->setupModelData(KGlobal::locale()->allCountriesList());
140  m_countrySort = new QSortFilterProxyModel(this);
141  m_countrySort->setSourceModel(countryModel);
142  m_countrySort->sort(0);
143 
144  m_languageModel = new LanguageModel(this);
145  m_languageModel->setupModelData(KGlobal::locale()->allLanguagesList());
146  m_languageSort = new QSortFilterProxyModel(this);
147  m_languageSort->setSourceModel(m_languageModel);
148  m_languageSort->sort(0);
149 
150  createFiles();
151  slotUpdateIntroductionNextButton();
152 }
153 
154 void MetalinkCreator::load()
155 {
156  KUrl url = KUrl(uiIntroduction.load->text());
157  if (uiIntroduction.loadButton->isChecked() && url.isValid())
158  {
159  if (!KGetMetalink::HandleMetalink::load(url, &metalink))
160  {
161  KMessageBox::error(this, i18n("Unable to load: %1", url.pathOrUrl()), i18n("Error"));
162  }
163  }
164 
165  m_general->load(metalink);
166  loadFiles();
167 }
168 
169 void MetalinkCreator::slotSave()
170 {
171  m_general->save(&metalink);
172 
173  KUrl url = KUrl(uiIntroduction.save->text());
174  if (url.isValid())
175  {
176  if(!KGetMetalink::HandleMetalink::save(url, &metalink))
177  {
178  KMessageBox::error(this, i18n("Unable to save to: %1", url.pathOrUrl()), i18n("Error"));
179  }
180  }
181 }
182 
183 void MetalinkCreator::createIntroduction()
184 {
185  QWidget *widget = new QWidget(this);
186  uiIntroduction.setupUi(widget);
187 
188  uiIntroduction.save->setFilter("*.meta4|" + i18n("Metalink Version 4.0 file (*.meta4)") + "\n*.metalink|" + i18n("Metalink Version 3.0 file (*.metalink)"));
189  uiIntroduction.save->fileDialog()->setOperationMode(KFileDialog::Saving);
190 
191  connect(uiIntroduction.save, SIGNAL(textChanged(QString)), this, SLOT(slotUpdateIntroductionNextButton()));
192  connect(uiIntroduction.load, SIGNAL(textChanged(QString)), this, SLOT(slotUpdateIntroductionNextButton()));
193  connect(uiIntroduction.loadButton, SIGNAL(toggled(bool)), this, SLOT(slotUpdateIntroductionNextButton()));
194 
195  m_introduction = addPage(widget, i18n("Define the saving location."));
196 
197  setValid(m_introduction, false);
198 }
199 
200 void MetalinkCreator::slotUpdateIntroductionNextButton()
201 {
202  bool enableNext = false;
203 
204  //check if a save location and if selected if also a load location has been specified and if the m_countrySort has been created
205  enableNext = uiIntroduction.save->url().isValid() && m_countrySort;
206  if (enableNext && uiIntroduction.loadButton->isChecked()) {
207  enableNext = uiIntroduction.load->url().isValid();
208  }
209 
210  setValid(m_introduction, enableNext);
211 }
212 
213 void MetalinkCreator::createFiles()
214 {
215  m_handler = new DirectoryHandler(this);
216  connect(m_handler, SIGNAL(finished()), this, SLOT(slotOpenDragDlg()));
217 
218  FileWidget *widget = new FileWidget(this);
219  uiFiles.setupUi(widget);
220 
221  m_filesModel = new QStandardItemModel(0, 1, this);
222  uiFiles.files->setModel(m_filesModel);
223 
224  uiFiles.infoWidget->setCloseButtonVisible(false);
225  uiFiles.infoWidget->setMessageType(KMessageWidget::Information);
226  uiFiles.add_local_file->setIcon(KIcon("list-add"));
227  uiFiles.add_file->setGuiItem(KStandardGuiItem::add());
228  uiFiles.properties_file->setGuiItem(KStandardGuiItem::properties());
229  uiFiles.properties_file->setEnabled(false);
230  uiFiles.remove_file->setGuiItem(KStandardGuiItem::remove());
231  uiFiles.remove_file->setEnabled(false);
232  uiFiles.dragDrop->hide();
233 
234  connect(uiFiles.add_local_file, SIGNAL(clicked(bool)), this, SLOT(slotAddLocalFilesClicked()));
235  connect(uiFiles.add_file, SIGNAL(clicked(bool)), this, SLOT(slotAddClicked()));
236  connect(uiFiles.remove_file, SIGNAL(clicked(bool)), this, SLOT(slotRemoveFile()));
237  connect(uiFiles.properties_file, SIGNAL(clicked(bool)), this, SLOT(slotFileProperties()));
238  connect(uiFiles.files->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(slotUpdateFilesButtons()));
239  connect(widget, SIGNAL(urlsDropped(QList<KUrl>)), m_handler, SLOT(slotFiles(QList<KUrl>)));
240 
241  addPage(widget, i18nc("file as in file on hard drive", "Files"));
242 }
243 
244 void MetalinkCreator::loadFiles()
245 {
246  foreach (const KGetMetalink::File &file, metalink.files.files)
247  {
248  QStandardItem *item = new QStandardItem(file.name);
249  if (!file.resources.isValid())
250  {
251  ++m_needUrlCount;
252  item->setIcon(KIcon("edit-delete"));
253  }
254  m_filesModel->insertRow(m_filesModel->rowCount(), item);
255  }
256 }
257 
258 void MetalinkCreator::slotUpdateFilesButtons()
259 {
260  const QModelIndexList indexes = uiFiles.files->selectionModel()->selectedRows();
261  uiFiles.remove_file->setEnabled(indexes.count());
262 
263  bool propertiesEnabled = (indexes.count() == 1);
264  uiFiles.properties_file->setEnabled(propertiesEnabled);
265 }
266 
267 void MetalinkCreator::slotAddLocalFilesClicked()
268 {
269  QPointer<KFileDialog> dialog = new KFileDialog(KUrl(), QString(), this);
270  dialog->setMode(KFile::Files | KFile::ExistingOnly | KFile::LocalOnly);
271  if (dialog->exec() == QDialog::Accepted) {
272  m_handler->slotFiles(dialog->selectedUrls());
273  }
274  delete dialog;
275 }
276 
277 void MetalinkCreator::slotAddFile()
278 {
279  QStandardItem *item = new QStandardItem(m_tempFile.name);
280  m_filesModel->insertRow(m_filesModel->rowCount(), item);
281  metalink.files.files.append(m_tempFile);
282  m_tempFile.clear();
283 
284  slotUpdateAssistantButtons(0, m_files);
285 }
286 
287 void MetalinkCreator::slotAddFile(const KGetMetalink::File &file)
288 {
289  QStandardItem *item = new QStandardItem(file.name);
290  if (!file.resources.isValid())
291  {
292  ++m_needUrlCount;
293  item->setIcon(KIcon("edit-delete"));
294  }
295  m_filesModel->insertRow(m_filesModel->rowCount(), item);
296  metalink.files.files.append(file);
297 
298  slotUpdateAssistantButtons(0, m_files);
299 }
300 
301 void MetalinkCreator::slotFileEdited(const QString &oldFileName, const QString &newFileName)
302 {
303  Q_UNUSED(oldFileName)
304 
305  const QModelIndex index = uiFiles.files->selectionModel()->selectedRows().first();
306  QStandardItem *item = m_filesModel->itemFromIndex(index);
307  item->setText(newFileName);
308 
309  //had no url but has it now
310  if (!item->icon().isNull())
311  {
312  --m_needUrlCount;
313  item->setIcon(KIcon());
314  }
315 
316  slotUpdateAssistantButtons(0, m_files);
317 }
318 
319 void MetalinkCreator::slotRemoveFile()
320 {
321  while (uiFiles.files->selectionModel()->hasSelection()) {
322  const QModelIndex index = uiFiles.files->selectionModel()->selectedRows().first();
323  const QString filePath = index.data().toString();
324  for (int i = 0; i < metalink.files.files.size(); ++i)
325  {
326  if (metalink.files.files.at(i).name == filePath)
327  {
328  //the entry had not url, so do not count it anymore
329  if (!index.data(Qt::DecorationRole).isNull())
330  {
331  --m_needUrlCount;
332  }
333  metalink.files.files.removeAt(i);
334  break;
335  }
336  }
337 
338  m_filesModel->removeRow(index.row());
339  }
340 
341  slotUpdateFilesButtons();
342  slotUpdateAssistantButtons(0, m_files);
343 }
344 
345 void MetalinkCreator::slotAddClicked()
346 {
347  //no old stored data should be used
348  m_tempFile.clear();
349  fileDlg(&m_tempFile);
350 }
351 
352 void MetalinkCreator::fileDlg(KGetMetalink::File *file, bool edit)
353 {
354  QStringList currentNames;
355  for (int i = 0; i < m_filesModel->rowCount(); ++i)
356  {
357  currentNames.append(m_filesModel->index(i, 0).data().toString());
358  }
359 
360  FileDlg *fileDlg = new FileDlg(file, currentNames, m_countrySort, m_languageSort, this, edit);
361  fileDlg->setAttribute(Qt::WA_DeleteOnClose);
362  fileDlg->setWindowModality(Qt::ApplicationModal);
363  fileDlg->show();
364 
365  connect(fileDlg, SIGNAL(addFile()), this, SLOT(slotAddFile()));
366  connect(fileDlg, SIGNAL(fileEdited(QString,QString)), this, SLOT(slotFileEdited(QString,QString)));
367 }
368 
369 void MetalinkCreator::slotFileProperties()
370 {
371  const QModelIndex index = uiFiles.files->selectionModel()->selectedRows().first();
372  const QString fileName = index.data().toString();
373 
374  //search the selected file in metalink
375  for (int i = 0; i < metalink.files.files.count(); ++i)
376  {
377  if (metalink.files.files.at(i).name == fileName)
378  {
379  fileDlg(&metalink.files.files[i], true);
380  break;
381  }
382  }
383 }
384 void MetalinkCreator::slotOpenDragDlg()
385 {
386  m_tempResources.clear();
387  m_tempCommonData.clear();
388  DragDlg *dragDlg = new DragDlg(&m_tempResources, &m_tempCommonData, m_countrySort, m_languageSort, this);
389  dragDlg->setAttribute(Qt::WA_DeleteOnClose);
390  dragDlg->show();
391 
392  connect(dragDlg, SIGNAL(usedTypes(QStringList,bool)), this, SLOT(slotHandleDropped(QStringList,bool)));
393 }
394 void MetalinkCreator::slotHandleDropped(const QStringList &types, bool createPartial)
395 {
396  uiFiles.progressBar->setMaximum(0);
397  uiFiles.dragDrop->show();
398  m_thread.setData(m_handler->takeFiles(), types, createPartial, m_tempResources, m_tempCommonData);
399 }
400 
401 void MetalinkCreator::slotThreadFinished()
402 {
403  uiFiles.progressBar->setMaximum(10);
404  uiFiles.dragDrop->hide();
405  slotUpdateAssistantButtons(0, m_files);
406 }
407 
408 #include "metalinkcreator.moc"
DirectoryHandler::slotFiles
void slotFiles(const QList< KUrl > &files)
The files the FileHandler should handle, the urls can also be urls to directories then the files of t...
Definition: filehandler.cpp:145
KGetMetalink::HandleMetalink::load
static bool load(const KUrl &destination, Metalink *metalink)
Loads destination into metalink.
Definition: metalinker.cpp:1232
metalinkcreator.h
localemodels.h
CountryModel::setupModelData
void setupModelData(const QStringList &countryCodes)
Definition: localemodels.cpp:64
FileWidget::FileWidget
FileWidget(QWidget *parent=0)
Definition: metalinkcreator.cpp:40
DirectoryHandler
Gets the name and the size of the files of a list of urls, also enters directories recursively if nee...
Definition: filehandler.h:68
KGetMetalink::File::resources
Resources resources
Definition: metalinker.h:272
MetalinkCreator::~MetalinkCreator
~MetalinkCreator()
Definition: metalinkcreator.cpp:94
GeneralWidget::load
void load(const KGetMetalink::Metalink &metalink) const
Definition: generalwidget.cpp:37
QWidget
DirectoryHandler::takeFiles
QList< FileData > takeFiles()
Returns the handled files and clears the member, call this after finished is emitted.
Definition: filehandler.cpp:137
FileWidget
Definition: metalinkcreator.h:44
QStandardItemModel
KGetMetalink::Files::files
QList< File > files
Definition: metalinker.h:296
KGetMetalink::File
Definition: metalinker.h:239
FileWidget::dropEvent
void dropEvent(QDropEvent *event)
Definition: metalinkcreator.cpp:55
FileDlg
Definition: filedlg.h:37
GeneralWidget
Definition: generalwidget.h:32
KAssistantDialog
KGetMetalink::Resources::clear
void clear()
Definition: metalinker.cpp:443
KGetMetalink::CommonData::clear
void clear()
Definition: metalinker.cpp:240
KGetMetalink::HandleMetalink::save
static bool save(const KUrl &destination, Metalink *metalink)
Saves metalink to destination.
Definition: metalinker.cpp:1296
MetalinkCreator::slotAddFile
void slotAddFile()
Adds m_tempFile to metalink and clears it for future reuse, also adds the filename to the filemodel...
Definition: metalinkcreator.cpp:277
KGetMetalink::Resources::isValid
bool isValid() const
Definition: metalinker.h:195
KGetMetalink::File::clear
void clear()
Definition: metalinker.cpp:595
filedlg.h
GeneralWidget::save
void save(KGetMetalink::Metalink *metalink)
Definition: generalwidget.cpp:88
DragDlg
Definition: dragdlg.h:37
FileWidget::dragEnterEvent
void dragEnterEvent(QDragEnterEvent *event)
Definition: metalinkcreator.cpp:47
MetalinkCreator::slotFileEdited
void slotFileEdited(const QString &oldFileName, const QString &newFileName)
This slot is used to update the filename in the filemodel if needed.
Definition: metalinkcreator.cpp:301
CountryModel
The following models are there to store the localized names and the codes of languages and countries ...
Definition: localemodels.h:35
generalwidget.h
QSortFilterProxyModel
MetalinkCreator::MetalinkCreator
MetalinkCreator(QWidget *parent=0)
Definition: metalinkcreator.cpp:71
KGetMetalink::Metalink::files
Files files
Definition: metalinker.h:329
KGetMetalink::File::name
QString name
Definition: metalinker.h:268
LanguageModel::setupModelData
void setupModelData(const QStringList &languageCodes)
Definition: localemodels.cpp:122
KGetMetalink::Metalink::isValid
bool isValid() const
checks if the minimum requirements of a metalink are met
Definition: metalinker.cpp:687
QStandardItem
FileWidget::urlsDropped
void urlsDropped(const QList< KUrl > &files)
MetalinkCreator::slotHandleDropped
void slotHandleDropped(const QStringList &types, bool createPartial)
Handles the dropped files, calls dialogs if needed etc.
Definition: metalinkcreator.cpp:394
LanguageModel
The LanguageModel stores localized names as well as the codes of languages.
Definition: localemodels.h:56
FileHandlerThread::setData
void setData(const QList< FileData > &files, const QStringList &types, bool createPartial, const KGetMetalink::Resources &tempResources, const KGetMetalink::CommonData &tempCommonData)
Definition: filehandler.cpp:40
dragdlg.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:53:17 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kget

Skip menu "kget"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



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