20 #include <QApplication>
23 #include <QStandardItemModel>
26 #include <KActionCollection>
28 #include <KIconLoader>
30 #include <KMessageBox>
33 #include <KStandardAction>
34 #include <KWindowSystem>
42 setAttribute(Qt::WA_DeleteOnClose);
43 setCaption(i18n(
"Import Links"));
47 KWindowInfo info = KWindowSystem::windowInfo(parent->winId(), NET::WMDesktop, NET::WMDesktop);
48 KWindowSystem::setCurrentDesktop(info.desktop());
49 KWindowSystem::forceActiveWindow(parent->winId());
54 m_proxyModel->setDynamicSortFilter(
true);
55 m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
66 ui.importLinks->setIcon(KIcon(
"document-import"));
73 ui.treeView->setModel(m_proxyModel);
74 ui.progressBar->hide();
77 m_patternSyntaxMenu =
new QMenu(i18nc(
"of a filter, e.g. RegExp or Wildcard",
"Pattern Syntax"),
this);
78 QAction *wildcardAction =
new QAction(i18n(
"Escape Sequences"),
this);
79 wildcardAction->setCheckable(
true);
81 QAction *regExpAction =
new QAction(i18n(
"Regular Expression"),
this);
82 regExpAction->setCheckable(
true);
84 QActionGroup *actionGroup =
new QActionGroup(
this);
85 actionGroup->addAction(wildcardAction);
86 actionGroup->addAction(regExpAction);
87 m_patternSyntaxMenu->addActions(actionGroup->actions());
90 QActionGroup *columnGroup =
new QActionGroup(
this);
91 m_nameAction =
new KAction(i18nc(
"name of a file",
"Name"),
this);
92 m_nameAction->setCheckable(
true);
93 m_nameAction->setChecked(
true);
94 m_urlAction =
new KAction(i18n(
"URL"),
this);
95 m_urlAction->setCheckable(
true);
96 columnGroup->addAction(m_nameAction);
97 columnGroup->addAction(m_urlAction);
98 connect(columnGroup, SIGNAL(triggered(QAction*)),
this, SLOT(slotFilterColumn(QAction*)));
100 connect(wildcardAction, SIGNAL(toggled(
bool)),
this, SLOT(wildcardPatternToggled(
bool)));
101 connect(ui.treeView, SIGNAL(doubleClicked(QModelIndex)),
102 this, SLOT(uncheckItem(QModelIndex)));
103 connect(ui.textFilter, SIGNAL(textChanged(QString)), SLOT(setTextFilter(QString)));
104 connect(ui.textFilter, SIGNAL(aboutToShowContextMenu(QMenu*)),
this, SLOT(contextMenuDisplayed(QMenu*)));
105 connect(ui.filterMode, SIGNAL(currentIndexChanged(
int)),
this, SLOT(slotFilterModeChanged(
int)));
106 connect(ui.showCombo, SIGNAL(currentIndexChanged(
int)),
this, SLOT(slotMimeTypeChanged(
int)));
107 connect(ui.showCombo, SIGNAL(currentIndexChanged(
int)), SLOT(updateSelectionButtons()));
108 connect(ui.urlRequester, SIGNAL(textChanged(QString)), SLOT(updateImportButtonStatus(QString)));
109 connect(ui.urlRequester, SIGNAL(urlSelected(KUrl)), SLOT(slotStartImport()));
110 connect(ui.selectAll, SIGNAL(clicked()),
this, SLOT(checkAll()));
111 connect(ui.deselectAll, SIGNAL(clicked()),
this, SLOT(uncheckAll()));
112 connect(ui.invertSelection, SIGNAL(clicked()),
this, SLOT(slotInvertSelection()));
113 connect(
this, SIGNAL(accepted()),
this, SLOT(slotStartLeech()));
114 connect(ui.showWebContent, SIGNAL(stateChanged(
int)), m_proxyModel, SLOT(setShowWebContent(
int)));
115 connect(ui.importLinks, SIGNAL(clicked()),
this, SLOT(slotStartImport()));
116 connect(ui.treeView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
117 SLOT(selectionChanged()));
118 connect(ui.dialogButtonBox, SIGNAL(rejected()), SLOT(reject()));
120 setMainWidget(widget);
121 QPushButton *download = ui.dialogButtonBox->addButton(i18nc(
"Download the items which have been selected",
"&Download"),
122 QDialogButtonBox::AcceptRole,
125 download->setIcon(KIcon(
"kget"));
132 delete(m_linkImporter);
135 void KGetLinkView::checkClipboard()
137 QString clipboardContent = QApplication::clipboard()->text(QClipboard::Clipboard);
139 if (clipboardContent.length() > 0)
141 delete m_linkImporter;
145 connect(m_linkImporter, SIGNAL(finished()), SLOT(slotImportFinished()));
153 showLinks(m_links,
false);
156 void KGetLinkView::showLinks(
const QStringList &links,
bool urlRequestVisible)
158 ui.importWidget->setVisible(urlRequestVisible);
162 model->setHeaderData(0, Qt::Horizontal, i18n(
"Auxiliary header"));
163 model->setHeaderData(1, Qt::Horizontal, i18n(
"File Name"));
164 model->setHeaderData(2, Qt::Horizontal, i18n(
"Description"));
165 model->setHeaderData(3, Qt::Horizontal, i18nc(
"list header: type of file",
"File Type"));
166 model->setHeaderData(4, Qt::Horizontal, i18n(
"Location (URL)"));
168 foreach (
const QString &linkitem, links)
173 if (linkitem.contains(QLatin1String(
"url "), Qt::CaseInsensitive) &&
174 linkitem.contains(QLatin1String(
"type "), Qt::CaseInsensitive)) {
175 const QStringList items = linkitem.split(QLatin1Char(
' '), QString::SkipEmptyParts);
176 const int count = items.count();
177 int index = items.indexOf(QLatin1String(
"url"));
178 if (index > -1 && index+1 < count)
179 url = items.at(index+1);
180 index = items.indexOf(QLatin1String(
"type"));
181 if (index > -1 && index+1 < count)
182 mt = KMimeType::mimeType(items.at(index+1));
185 mt = KMimeType::findByUrl(linkitem, 0,
true,
true);
188 kDebug(5001) <<
"Adding:" << linkitem;
190 QString file = url.fileName();
192 file = QString(url.host());
194 QString mimeTypeName, mimeTypeIcon, mimeTypeComment;
196 mimeTypeName = mt->name();
197 mimeTypeIcon = mt->iconName();
198 mimeTypeComment = mt->comment();
202 item->setIcon(KIcon(mimeTypeIcon));
203 item->setCheckable(
true);
204 item->setCheckState(Qt::Checked);
205 item->setData(QVariant(url.fileName()), Qt::DisplayRole);
206 item->setData(QVariant(mimeTypeName), Qt::UserRole);
208 QList<QStandardItem*> items;
210 number->setData(model->rowCount(), Qt::DisplayRole);
217 model->insertRow(model->rowCount(), items);
220 connect(model, SIGNAL(itemChanged(
QStandardItem*)),
this, SLOT(selectionChanged()));
221 m_proxyModel->setSourceModel(model);
222 m_proxyModel->setFilterKeyColumn(1);
223 m_proxyModel->sort(0);
225 ui.treeView->header()->hideSection(0);
226 ui.treeView->setColumnWidth(1, 200);
231 void KGetLinkView::slotMimeTypeChanged(
int index)
233 m_proxyModel->
setFilterType(ui.showCombo->itemData(index).toInt());
236 void KGetLinkView::slotFilterModeChanged(
int index)
238 m_proxyModel->
setFilterMode(ui.filterMode->itemData(index).toInt());
241 void KGetLinkView::slotFilterColumn(QAction *action)
247 void KGetLinkView::slotStartLeech()
254 for (
int row = 0; row < model->rowCount(); row++)
258 if (checkeableItem->checkState() == Qt::Checked)
260 urls.append(KUrl(model->data(model->index(row, 4)).toString()));
270 setPlainCaption( i18n(
"Links in: %1 - KGet", url ) );
277 KUrl clipboardUrl = KUrl(QApplication::clipboard()->text(QClipboard::Clipboard).trimmed());
278 if (clipboardUrl.isValid() &&
279 ((!clipboardUrl.scheme().isEmpty() && !clipboardUrl.host().isEmpty()) ||
280 (clipboardUrl.isLocalFile())))
282 ui.urlRequester->setUrl(clipboardUrl);
287 ui.urlRequester->setUrl(KUrl(url));
292 void KGetLinkView::selectionChanged()
297 const int modelRowCount = model->rowCount();
298 bool buttonEnabled =
false;
301 for (
int row = 0; row < modelRowCount; row++)
305 if ((checkeableItem->checkState() == Qt::Checked))
307 buttonEnabled =
true;
310 if (m_proxyModel->mapFromSource(model->index(row, 1)).isValid())
317 ui.selectAll->setEnabled( !(!modelRowCount || count == m_proxyModel->rowCount() ) );
318 ui.deselectAll->setEnabled( count > 0 );
319 ui.invertSelection->setEnabled( count > 0 );
321 enableButtonOk(buttonEnabled);
325 void KGetLinkView::setTextFilter(
const QString &text)
328 QString temp = text.isEmpty() ? ui.textFilter->text() : text;
330 m_proxyModel->setFilterWildcard(temp);
332 QRegExp rx(temp, Qt::CaseSensitive, QRegExp::RegExp2);
333 m_proxyModel->setFilterRegExp(rx);
336 updateSelectionButtons();
339 void KGetLinkView::updateSelectionButtons()
342 ui.selectAll->setText(isFiltered ? i18n(
"&Select All Filtered") : i18n(
"&Select All"));
343 ui.deselectAll->setText(isFiltered ? i18n(
"D&eselect All Filtered") : i18n(
"D&eselect All"));
348 void KGetLinkView::checkAll()
353 for (
int row = 0; row < m_proxyModel->rowCount(); row++)
355 const QModelIndex index = m_proxyModel->mapToSource(m_proxyModel->index(row, 3));
357 item->setCheckState(Qt::Checked);
362 void KGetLinkView::uncheckAll()
367 for (
int row = 0; row < m_proxyModel->rowCount(); row++)
369 const QModelIndex index = m_proxyModel->mapToSource(m_proxyModel->index(row, 3));
371 item->setCheckState(Qt::Unchecked);
376 void KGetLinkView::uncheckItem(
const QModelIndex &index)
381 if (index.column() != 0)
383 QStandardItem *item = model->itemFromIndex(model->index(m_proxyModel->mapToSource(index).row(), 1));
384 item->setCheckState(item->checkState() == Qt::Checked ? Qt::Unchecked : Qt::Checked);
389 void KGetLinkView::slotCheckSelected()
394 foreach(
const QModelIndex &index, ui.treeView->selectionModel()->selectedIndexes())
396 QModelIndex sourceIndex = m_proxyModel->mapToSource(index);
399 item->setCheckState(Qt::Checked);
404 void KGetLinkView::slotInvertSelection()
409 for (
int row = 0; row < m_proxyModel->rowCount(); row++)
411 const QModelIndex index = m_proxyModel->mapToSource(m_proxyModel->index(row, 3));
413 item->setCheckState((item->checkState() == Qt::Checked) ? Qt::Unchecked : Qt::Checked);
418 void KGetLinkView::slotStartImport()
420 delete m_linkImporter;
422 m_linkImporter =
new LinkImporter(ui.urlRequester->url(),
this);
424 connect(m_linkImporter, SIGNAL(progress(
int)), SLOT(slotImportProgress(
int)));
425 connect(m_linkImporter, SIGNAL(finished()), SLOT(slotImportFinished()));
427 if (!ui.urlRequester->url().isLocalFile())
432 m_linkImporter->start();
433 ui.progressBar->show();
436 void KGetLinkView::slotImportProgress(
int progress)
438 ui.progressBar->setValue(progress);
441 void KGetLinkView::slotImportFinished()
443 ui.progressBar->hide();
444 m_links = m_linkImporter->
links();
445 showLinks(m_links,
true);
448 void KGetLinkView::updateImportButtonStatus(
const QString &text)
450 bool enabled =
false;
459 ui.importLinks->setEnabled(enabled);
462 void KGetLinkView::contextMenuDisplayed(QMenu *menu)
464 menu->addSeparator();
465 menu->addMenu(m_patternSyntaxMenu);
466 menu->addSeparator()->setText(i18n(
"Filter Column"));
467 menu->addAction(m_nameAction);
468 menu->addAction(m_urlAction);
472 void KGetLinkView::wildcardPatternToggled(
bool enabled)
481 #include "kget_linkview.moc"
QList< QString > links()
Returns a list with the links of the selected url m_url.
void setPageUrl(const QString &url)
void copyRemoteFile()
copy the remote file out of the thread
static void setLinkViewFilterPatternSyntax(int v)
Set LinkViewFilterPatternSyntax.
void checkClipboard(const QString &clipboardContent)
Check for urls in clipboard.
KGetLinkView(QWidget *parent=0)
Import a list of urls from a file (local or remote)
static int linkViewFilterPatternSyntax()
Get LinkViewFilterPatternSyntax.
void importUrl(const QString &url=QString())
void setShowWebContent(bool show)
Subclass to make sure that the size of the dialog is automatically stored and restored.
void setFilterType(int filterType)
static void showNewTransferDialog(const KUrl &url=KUrl())
void setFilterMode(int filterMode)
void setFilterColumn(int column)
void setLinks(const QStringList &links)