File indexing completed on 2024-04-14 15:05:24

0001 /***************************************************************************
0002     Private classes for the bookmark handler
0003                              -------------------
0004     begin                : Sun Mar 20 2011
0005     copyright            : (C) 2011-2018 by Alexander Reinholdt
0006     email                : alexander.reinholdt@kdemail.net
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *   This program is free software; you can redistribute it and/or modify  *
0011  *   it under the terms of the GNU General Public License as published by  *
0012  *   the Free Software Foundation; either version 2 of the License, or     *
0013  *   (at your option) any later version.                                   *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful, but   *
0016  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
0018  *   General Public License for more details.                              *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program; if not, write to the                         *
0022  *   Free Software Foundation, 51 Franklin Street, Suite 500, Boston,      *
0023  *   MA 02110-1335, USA                                                    *
0024  ***************************************************************************/
0025 
0026 #ifdef HAVE_CONFIG_H
0027 #include <config.h>
0028 #endif
0029 
0030 // application specific includes
0031 #include "smb4kbookmarkhandler_p.h"
0032 #include "smb4ksettings.h"
0033 #include "smb4kbookmark.h"
0034 
0035 // Qt includes
0036 #include <QEvent>
0037 #include <QTimer>
0038 #include <QVBoxLayout>
0039 #include <QHBoxLayout>
0040 #include <QGridLayout>
0041 #include <QLabel>
0042 #include <QHeaderView>
0043 #include <QTreeWidgetItemIterator>
0044 #include <QPushButton>
0045 #include <QMenu>
0046 #include <QInputDialog>
0047 #include <QDialogButtonBox>
0048 #include <QDropEvent>
0049 #include <QDragMoveEvent>
0050 #include <QDragEnterEvent>
0051 #include <QDragLeaveEvent>
0052 #include <QWindow>
0053 
0054 // KDE includes
0055 #define TRANSLATION_DOMAIN "smb4k-core"
0056 #include <KI18n/KLocalizedString>
0057 #include <KIconThemes/KIconLoader>
0058 #include <KConfigGui/KWindowConfig>
0059 
0060 
0061 Smb4KBookmarkDialog::Smb4KBookmarkDialog(const QList<BookmarkPtr> &bookmarks, const QStringList &groups, QWidget *parent)
0062 : QDialog(parent)
0063 {
0064   //
0065   // Set the window title
0066   // 
0067   setWindowTitle(i18n("Add Bookmarks"));
0068 
0069   //
0070   // Setup the view
0071   // 
0072   setupView();
0073   
0074   //
0075   // Load the list of bookmarks and groups
0076   // 
0077   loadLists(bookmarks, groups);
0078 
0079   //
0080   // Set the dialog size
0081   // 
0082   create();
0083 
0084   KConfigGroup group(Smb4KSettings::self()->config(), "BookmarkDialog");
0085   QSize dialogSize;
0086   
0087   if (group.exists())
0088   {
0089     KWindowConfig::restoreWindowSize(windowHandle(), group);
0090     dialogSize = windowHandle()->size();
0091   }
0092   else
0093   {
0094     dialogSize = sizeHint();
0095   }
0096   
0097   resize(dialogSize); // workaround for QTBUG-40584
0098 
0099   //
0100   // Fill the completion objects
0101   // 
0102   m_label_edit->completionObject()->setItems(group.readEntry("LabelCompletion", QStringList()));
0103   m_group_combo->completionObject()->setItems(group.readEntry("GroupCompletion", m_groups));
0104 
0105   //
0106   // Connections
0107   // 
0108   connect(KIconLoader::global(), SIGNAL(iconChanged(int)), SLOT(slotIconSizeChanged(int)));
0109 }
0110 
0111 
0112 Smb4KBookmarkDialog::~Smb4KBookmarkDialog()
0113 {
0114   while (!m_bookmarks.isEmpty())
0115   {
0116     m_bookmarks.takeFirst().clear();
0117   }
0118 }
0119 
0120 
0121 const QList<BookmarkPtr> &Smb4KBookmarkDialog::bookmarks()
0122 {
0123   return m_bookmarks;
0124 }
0125 
0126 
0127 void Smb4KBookmarkDialog::setupView()
0128 {
0129   QVBoxLayout *layout = new QVBoxLayout(this);
0130   layout->setSpacing(5);
0131 
0132   QWidget *description = new QWidget(this);
0133 
0134   QHBoxLayout *desc_layout = new QHBoxLayout(description);
0135   desc_layout->setSpacing(5);
0136   desc_layout->setMargin(0);
0137 
0138   QLabel *pixmap = new QLabel(description);
0139   QPixmap sync_pix = KDE::icon("bookmark-new").pixmap(KIconLoader::SizeHuge);
0140   pixmap->setPixmap(sync_pix);
0141   pixmap->setAlignment(Qt::AlignBottom);
0142 
0143   QLabel *label = new QLabel(i18n("All listed shares will be bookmarked. To edit the label "
0144                                   "or group, click the respective bookmark entry."), description);
0145   label->setWordWrap(true);
0146   label->setAlignment(Qt::AlignBottom);
0147 
0148   desc_layout->addWidget(pixmap, 0);
0149   desc_layout->addWidget(label, Qt::AlignBottom);
0150 
0151   m_widget = new QListWidget(this);
0152   m_widget->setSortingEnabled(true);
0153   m_widget->setSelectionMode(QAbstractItemView::SingleSelection);
0154   int icon_size = KIconLoader::global()->currentSize(KIconLoader::Small);
0155   m_widget->setIconSize(QSize(icon_size, icon_size));
0156 
0157   m_editors = new QWidget(this);
0158   m_editors->setEnabled(false);
0159 
0160   QGridLayout *editors_layout = new QGridLayout(m_editors);
0161   editors_layout->setSpacing(5);
0162   editors_layout->setMargin(0);
0163 
0164   QLabel *l_label = new QLabel(i18n("Label:"), m_editors);
0165   m_label_edit = new KLineEdit(m_editors);
0166   m_label_edit->setClearButtonEnabled(true);
0167 
0168   QLabel *g_label = new QLabel(i18n("Group:"), m_editors);
0169   m_group_combo = new KComboBox(true, m_editors);
0170 
0171   editors_layout->addWidget(l_label, 0, 0, 0);
0172   editors_layout->addWidget(m_label_edit, 0, 1, 0);
0173   editors_layout->addWidget(g_label, 1, 0, 0);
0174   editors_layout->addWidget(m_group_combo, 1, 1, 0);
0175   
0176   QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal, this);
0177   m_ok_button = buttonBox->addButton(QDialogButtonBox::Ok);
0178   m_cancel_button = buttonBox->addButton(QDialogButtonBox::Cancel);
0179   
0180   m_ok_button->setShortcut(Qt::CTRL|Qt::Key_Return);
0181   m_cancel_button->setShortcut(Qt::Key_Escape);
0182   
0183   m_ok_button->setDefault(true);
0184 
0185   layout->addWidget(description, 0);
0186   layout->addWidget(m_widget, 0);
0187   layout->addWidget(m_editors, 0);
0188   layout->addWidget(buttonBox, 0);
0189 
0190   //
0191   // Connections
0192   // 
0193   connect(m_widget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(slotBookmarkClicked(QListWidgetItem*)));
0194   connect(m_label_edit, SIGNAL(editingFinished()), this, SLOT(slotLabelEdited()));
0195   connect(m_group_combo->lineEdit(), SIGNAL(editingFinished()), this, SLOT(slotGroupEdited()));
0196   connect(m_ok_button, SIGNAL(clicked()), this, SLOT(slotDialogAccepted()));
0197   connect(m_cancel_button, SIGNAL(clicked()), this, SLOT(reject()));
0198 }
0199 
0200 
0201 void Smb4KBookmarkDialog::loadLists(const QList<BookmarkPtr> &bookmarks, const QStringList &groups)
0202 {
0203   // Copy the bookmarks to the internal list and add them to 
0204   // the list widget afterwards.
0205   for (const BookmarkPtr &b : bookmarks)
0206   {
0207     QListWidgetItem *item = new QListWidgetItem(b->icon(), b->displayString(), m_widget);
0208     item->setData(Qt::UserRole, static_cast<QUrl>(b->url()));
0209     
0210     m_bookmarks << b;
0211   }
0212 
0213   m_groups = groups;
0214   m_group_combo->addItems(m_groups);
0215 }
0216 
0217 
0218 BookmarkPtr Smb4KBookmarkDialog::findBookmark(const QUrl &url)
0219 {
0220   BookmarkPtr bookmark;
0221   
0222   for (const BookmarkPtr &b : m_bookmarks)
0223   {
0224     if (b->url() == url)
0225     {
0226       bookmark = b;
0227       break;
0228     }
0229     else
0230     {
0231       continue;
0232     }
0233   }
0234 
0235   return bookmark;
0236 }
0237 
0238 
0239 void Smb4KBookmarkDialog::slotBookmarkClicked(QListWidgetItem *bookmark_item)
0240 {
0241   if (bookmark_item)
0242   {
0243     // Enable the editor widgets if necessary
0244     if (!m_editors->isEnabled())
0245     {
0246       m_editors->setEnabled(true);
0247     }
0248 
0249     QUrl url = bookmark_item->data(Qt::UserRole).toUrl();
0250     
0251     BookmarkPtr bookmark = findBookmark(url);
0252 
0253     if (bookmark)
0254     {
0255       m_label_edit->setText(bookmark->label());
0256       m_group_combo->setCurrentItem(bookmark->groupName());
0257     }
0258     else
0259     {
0260       m_label_edit->clear();
0261       m_group_combo->clearEditText();
0262       m_editors->setEnabled(false);
0263     }
0264   }
0265   else
0266   {
0267     m_label_edit->clear();
0268     m_group_combo->clearEditText();
0269     m_editors->setEnabled(false);
0270   }
0271 }
0272 
0273 
0274 void Smb4KBookmarkDialog::slotLabelEdited()
0275 {
0276   // Set the label
0277   QUrl url = m_widget->currentItem()->data(Qt::UserRole).toUrl();
0278 
0279   BookmarkPtr bookmark = findBookmark(url);
0280 
0281   if (bookmark)
0282   {
0283     bookmark->setLabel(m_label_edit->userText());
0284   }
0285 
0286   // Add label to completion object
0287   KCompletion *completion = m_label_edit->completionObject();
0288 
0289   if (!m_label_edit->userText().isEmpty())
0290   {
0291     completion->addItem(m_label_edit->userText());
0292   }
0293 }
0294 
0295 
0296 void Smb4KBookmarkDialog::slotGroupEdited()
0297 {
0298   // Set the group
0299   QUrl url = m_widget->currentItem()->data(Qt::UserRole).toUrl();
0300   
0301   BookmarkPtr bookmark = findBookmark(url);
0302 
0303   if (bookmark)
0304   {
0305     bookmark->setGroupName(m_group_combo->currentText());
0306   }
0307 
0308   // Add the group name to the combo box
0309   if (m_group_combo->findText(m_group_combo->currentText()) == -1)
0310   {
0311     m_group_combo->addItem(m_group_combo->currentText());
0312   }
0313 
0314   // Add group to completion object
0315   KCompletion *completion = m_group_combo->completionObject();
0316 
0317   if (!m_group_combo->currentText().isEmpty())
0318   {
0319     completion->addItem(m_group_combo->currentText());
0320   }
0321 }
0322 
0323 
0324 void Smb4KBookmarkDialog::slotDialogAccepted()
0325 {
0326   KConfigGroup group(Smb4KSettings::self()->config(), "BookmarkDialog");
0327   KWindowConfig::saveWindowSize(windowHandle(), group);
0328   group.writeEntry("LabelCompletion", m_label_edit->completionObject()->items());
0329   group.writeEntry("GroupCompletion", m_group_combo->completionObject()->items());
0330   
0331   accept();
0332 }
0333 
0334 
0335 void Smb4KBookmarkDialog::slotIconSizeChanged(int group)
0336 {
0337   switch (group)
0338   {
0339     case KIconLoader::Small:
0340     {
0341       int icon_size = KIconLoader::global()->currentSize(KIconLoader::Small);
0342       m_widget->setIconSize(QSize(icon_size, icon_size));
0343       break;
0344     }
0345     default:
0346     {
0347       break;
0348     }
0349   }
0350 }
0351 
0352 
0353 Smb4KBookmarkEditor::Smb4KBookmarkEditor(const QList<BookmarkPtr> &bookmarks, QWidget *parent)
0354 : QDialog(parent), m_bookmarks(bookmarks)
0355 {
0356   //
0357   // Set the window title
0358   // 
0359   setWindowTitle(i18n("Edit Bookmarks"));
0360   
0361   //
0362   // Setup the view
0363   // 
0364   setupView();
0365   
0366   //
0367   // Load the bookmarks into the editor
0368   // 
0369   loadBookmarks();
0370 
0371   //
0372   // Set the dialog size
0373   // 
0374   create();
0375 
0376   KConfigGroup group(Smb4KSettings::self()->config(), "BookmarkEditor");
0377   QSize dialogSize;
0378   
0379   if (group.exists())
0380   {
0381     KWindowConfig::restoreWindowSize(windowHandle(), group);
0382     dialogSize = windowHandle()->size();
0383   }
0384   else
0385   {
0386     dialogSize = sizeHint();
0387   }
0388   
0389   resize(dialogSize); // workaround for QTBUG-40584
0390   
0391   //
0392   // Fill the completion objects
0393   // 
0394   m_label_edit->completionObject()->setItems(group.readEntry("LabelCompletion", QStringList()));
0395   m_login_edit->completionObject()->setItems(group.readEntry("LoginCompletion", QStringList()));
0396   m_ip_edit->completionObject()->setItems(group.readEntry("IPCompletion", QStringList()));
0397   m_group_combo->completionObject()->setItems(group.readEntry("GroupCompletion", m_groups));
0398 
0399   //
0400   // Connections
0401   // 
0402   connect(KIconLoader::global(), SIGNAL(iconChanged(int)), SLOT(slotIconSizeChanged(int)));
0403 }
0404 
0405 
0406 Smb4KBookmarkEditor::~Smb4KBookmarkEditor()
0407 {
0408   while (!m_bookmarks.isEmpty())
0409   {
0410     m_bookmarks.takeFirst().clear();
0411   }
0412 }
0413 
0414 
0415 bool Smb4KBookmarkEditor::eventFilter(QObject *obj, QEvent *e)
0416 {
0417   if (obj == m_tree_widget->viewport())
0418   {
0419     switch (e->type())
0420     {
0421       case QEvent::DragEnter:
0422       {
0423         QDragEnterEvent *ev = static_cast<QDragEnterEvent *>(e);
0424         
0425         if (ev->source() == m_tree_widget->viewport())
0426         {
0427           e->accept();
0428         }
0429         else
0430         {
0431           e->ignore();
0432         }
0433         break;
0434       }
0435       case QEvent::DragLeave:
0436       {
0437         e->ignore();
0438         break;
0439       }
0440       case QEvent::Drop:
0441       {
0442         QTimer::singleShot(50, this, SLOT(slotAdjust()));
0443         break;
0444       }
0445       default:
0446       {
0447         break;
0448       }
0449     }
0450   }
0451   
0452   return QDialog::eventFilter(obj, e);
0453 }
0454 
0455 
0456 void Smb4KBookmarkEditor::setupView()
0457 {
0458   QVBoxLayout *layout = new QVBoxLayout(this);
0459   layout->setSpacing(5);
0460 
0461   m_tree_widget = new QTreeWidget(this);
0462   m_tree_widget->setColumnCount(2);
0463   m_tree_widget->hideColumn((m_tree_widget->columnCount() - 1)); // for sorting purposes
0464   m_tree_widget->headerItem()->setHidden(true);
0465   m_tree_widget->setRootIsDecorated(true);
0466   m_tree_widget->setSelectionMode(QAbstractItemView::SingleSelection);
0467   m_tree_widget->setContextMenuPolicy(Qt::CustomContextMenu);
0468   m_tree_widget->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
0469   m_tree_widget->setDragDropMode(QTreeWidget::InternalMove);
0470   int icon_size = KIconLoader::global()->currentSize(KIconLoader::Small);
0471   m_tree_widget->setIconSize(QSize(icon_size, icon_size));
0472   m_tree_widget->viewport()->installEventFilter(this);
0473 
0474   m_add_group = new QAction(KDE::icon("bookmark-add-folder"), i18n("Add Group"), m_tree_widget);
0475   m_delete = new QAction(KDE::icon("edit-delete"), i18n("Remove"), m_tree_widget);
0476   m_clear = new QAction(KDE::icon("edit-clear"), i18n("Clear"), m_tree_widget);
0477   
0478   m_menu = new KActionMenu(m_tree_widget);
0479   m_menu->addAction(m_add_group);
0480   m_menu->addAction(m_delete);
0481   m_menu->addAction(m_clear);
0482 
0483   m_editors = new QWidget(this);
0484   m_editors->setEnabled(false);
0485 
0486   QGridLayout *editors_layout = new QGridLayout(m_editors);
0487   editors_layout->setSpacing(5);
0488   editors_layout->setMargin(0);
0489 
0490   QLabel *l_label = new QLabel(i18n("Label:"), m_editors);
0491   m_label_edit = new KLineEdit(m_editors);
0492   m_label_edit->setClearButtonEnabled(true);
0493 
0494   QLabel *lg_label = new QLabel(i18n("Login:"), m_editors);
0495   m_login_edit = new KLineEdit(m_editors);
0496   m_login_edit->setClearButtonEnabled(true);
0497 
0498   QLabel *i_label = new QLabel(i18n("IP Address:"), m_editors);
0499   m_ip_edit = new KLineEdit(m_editors);
0500   m_ip_edit->setClearButtonEnabled(true);
0501   
0502   QLabel *g_label = new QLabel(i18n("Group:"), m_editors);
0503   m_group_combo = new KComboBox(true, m_editors);
0504   m_group_combo->setDuplicatesEnabled(false);
0505 
0506   editors_layout->addWidget(l_label, 0, 0, 0);
0507   editors_layout->addWidget(m_label_edit, 0, 1, 0);
0508   editors_layout->addWidget(lg_label, 1, 0, 0);
0509   editors_layout->addWidget(m_login_edit, 1, 1, 0);
0510   editors_layout->addWidget(i_label, 2, 0, 0);
0511   editors_layout->addWidget(m_ip_edit, 2, 1, 0);
0512   editors_layout->addWidget(g_label, 3, 0, 0);
0513   editors_layout->addWidget(m_group_combo, 3, 1, 0);
0514   
0515   QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal, this);
0516   m_ok_button = buttonBox->addButton(QDialogButtonBox::Ok);
0517   m_cancel_button = buttonBox->addButton(QDialogButtonBox::Cancel);
0518   
0519   m_ok_button->setShortcut(Qt::CTRL|Qt::Key_Return);
0520   m_cancel_button->setShortcut(Qt::Key_Escape);
0521 
0522   m_ok_button->setDefault(true);
0523 
0524   layout->addWidget(m_tree_widget);
0525   layout->addWidget(m_editors);
0526   layout->addWidget(buttonBox);
0527 
0528   //
0529   // Connections
0530   // 
0531   connect(m_tree_widget, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(slotItemClicked(QTreeWidgetItem*,int)));
0532   connect(m_tree_widget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slotContextMenuRequested(QPoint)));
0533   connect(m_label_edit, SIGNAL(editingFinished()), this, SLOT(slotLabelEdited()));
0534   connect(m_ip_edit, SIGNAL(editingFinished()), this, SLOT(slotIPEdited()));
0535   connect(m_login_edit, SIGNAL(editingFinished()), this, SLOT(slotLoginEdited()));
0536   connect(m_group_combo->lineEdit(), SIGNAL(editingFinished()), this, SLOT(slotGroupEdited()));
0537   connect(m_add_group, SIGNAL(triggered(bool)), this, SLOT(slotAddGroupTriggered(bool)));
0538   connect(m_delete, SIGNAL(triggered(bool)), this, SLOT(slotDeleteTriggered(bool)));
0539   connect(m_clear, SIGNAL(triggered(bool)), this, SLOT(slotClearTriggered(bool)));
0540   connect(m_ok_button, SIGNAL(clicked()), this, SLOT(slotDialogAccepted()));
0541   connect(m_cancel_button, SIGNAL(clicked()), this, SLOT(slotDialogRejected()));
0542 }
0543 
0544 
0545 void Smb4KBookmarkEditor::loadBookmarks()
0546 {
0547   //
0548   // Clear the tree widget and the group combo box
0549   //
0550   m_tree_widget->clear();
0551   m_group_combo->clear();
0552     
0553   // 
0554   // Copy the groups into the internal list
0555   // 
0556   m_groups.clear();
0557   
0558   for (const BookmarkPtr &bookmark : m_bookmarks)
0559   {
0560     if (!m_groups.contains(bookmark->groupName()))
0561     {
0562       m_groups << bookmark->groupName();
0563     }
0564   }
0565   
0566   //
0567   // Insert the groups into the tree widget
0568   // 
0569   for (const QString &group : m_groups)
0570   {
0571     if (!group.isEmpty())
0572     {
0573       QTreeWidgetItem *groupItem = new QTreeWidgetItem(QTreeWidgetItem::UserType);
0574       groupItem->setIcon(0, KDE::icon("folder-bookmark"));
0575       groupItem->setText(0, group);
0576       groupItem->setText((m_tree_widget->columnCount() - 1), QString("00_%1").arg(group));
0577       groupItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsDropEnabled);
0578       m_tree_widget->addTopLevelItem(groupItem);
0579     }
0580   }
0581   
0582   // 
0583   // Insert the bookmarks info the tree widget
0584   // 
0585   for (const BookmarkPtr &bookmark : m_bookmarks)
0586   {
0587     QTreeWidgetItem *bookmarkItem = new QTreeWidgetItem(QTreeWidgetItem::UserType);
0588     bookmarkItem->setData(0, QTreeWidgetItem::UserType, static_cast<QUrl>(bookmark->url()));
0589     bookmarkItem->setIcon(0, bookmark->icon());
0590     bookmarkItem->setText(0, bookmark->displayString());
0591     bookmarkItem->setText((m_tree_widget->columnCount() - 1), QString("01_%1").arg(bookmark->url().toString(QUrl::RemoveUserInfo|QUrl::RemovePort)));
0592     bookmarkItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsDragEnabled);
0593     
0594     if (!bookmark->groupName().isEmpty())
0595     {
0596       QList<QTreeWidgetItem *> items = m_tree_widget->findItems(bookmark->groupName(), Qt::MatchFixedString|Qt::MatchCaseSensitive, 0);
0597       
0598       if (!items.isEmpty())
0599       {
0600         items.first()->addChild(bookmarkItem);
0601         items.first()->setExpanded(true);
0602       }
0603     }
0604     else
0605     {
0606       m_tree_widget->addTopLevelItem(bookmarkItem);
0607     }
0608   }
0609 
0610   // 
0611   // Sort
0612   // 
0613   for (int i = 0; i < m_tree_widget->topLevelItemCount(); ++i)
0614   {
0615     m_tree_widget->topLevelItem(i)->sortChildren((m_tree_widget->columnCount() - 1), Qt::AscendingOrder);
0616   }
0617   
0618   m_tree_widget->sortItems((m_tree_widget->columnCount() - 1), Qt::AscendingOrder);
0619   
0620   //
0621   // Check that an empty group entry is also present. If it is not there,
0622   // add it now and insert the groups to the group combo box afterwards.
0623   // 
0624   if (!m_groups.contains("") && !m_groups.contains(QString()))
0625   {
0626     m_groups << "";
0627   }
0628   
0629   m_group_combo->addItems(m_groups);
0630   m_group_combo->setCurrentItem("");
0631 }
0632 
0633 
0634 QList<BookmarkPtr> Smb4KBookmarkEditor::editedBookmarks()
0635 {
0636   return m_bookmarks;
0637 }
0638 
0639 
0640 
0641 BookmarkPtr Smb4KBookmarkEditor::findBookmark(const QUrl &url)
0642 {
0643   BookmarkPtr bookmark;
0644 
0645   for (const BookmarkPtr &b : m_bookmarks)
0646   {
0647     if (b->url() == url)
0648     {
0649       bookmark = b;
0650       break;
0651     }
0652     else
0653     {
0654       continue;
0655     }
0656   }
0657 
0658   return bookmark;
0659 }
0660 
0661 
0662 void Smb4KBookmarkEditor::slotItemClicked(QTreeWidgetItem *item, int /*col*/)
0663 {
0664   if (item)
0665   {
0666     if (m_tree_widget->indexOfTopLevelItem(item) != -1)
0667     {
0668       // This is a top-level item, i.e. it is either a bookmark without
0669       // group or a group entry.
0670       // Bookmarks have an URL stored, group folders not.
0671       if (!item->data(0, QTreeWidgetItem::UserType).toUrl().isEmpty())
0672       {
0673         BookmarkPtr bookmark = findBookmark(item->data(0, QTreeWidgetItem::UserType).toUrl());
0674 
0675         if (bookmark)
0676         {
0677           m_label_edit->setText(bookmark->label());
0678           m_login_edit->setText(bookmark->login());
0679           m_ip_edit->setText(bookmark->hostIpAddress());
0680           m_group_combo->setCurrentItem(bookmark->groupName());
0681           m_editors->setEnabled(true);
0682         }
0683         else
0684         {
0685           m_label_edit->clear();
0686           m_login_edit->clear();
0687           m_ip_edit->clear();
0688           m_group_combo->clearEditText();
0689           m_editors->setEnabled(false);
0690         }
0691       }
0692       else
0693       {
0694         m_label_edit->clear();
0695         m_login_edit->clear();
0696         m_ip_edit->clear();
0697         m_group_combo->clearEditText();
0698         m_editors->setEnabled(false);
0699       }
0700     }
0701     else
0702     {
0703       // This can only be a bookmark.
0704       BookmarkPtr bookmark = findBookmark(item->data(0, QTreeWidgetItem::UserType).toUrl());
0705 
0706       if (bookmark)
0707       {
0708         m_label_edit->setText(bookmark->label());
0709         m_login_edit->setText(bookmark->login());
0710         m_ip_edit->setText(bookmark->hostIpAddress());
0711         m_group_combo->setCurrentItem(bookmark->groupName());
0712         m_editors->setEnabled(true);
0713       }
0714       else
0715       {
0716         m_label_edit->clear();
0717         m_login_edit->clear();
0718         m_ip_edit->clear();
0719         m_group_combo->clearEditText();
0720         m_editors->setEnabled(false);
0721       }
0722     }
0723   }
0724   else
0725   {
0726     m_label_edit->clear();
0727     m_login_edit->clear();
0728     m_ip_edit->clear();
0729     m_group_combo->clearEditText();
0730     m_editors->setEnabled(false);
0731   }
0732 }
0733 
0734 
0735 void Smb4KBookmarkEditor::slotContextMenuRequested(const QPoint &pos)
0736 {
0737   QTreeWidgetItem *item = m_tree_widget->itemAt(pos);
0738   m_delete->setEnabled((item));
0739   m_menu->menu()->popup(m_tree_widget->viewport()->mapToGlobal(pos));  
0740 }
0741 
0742 
0743 void Smb4KBookmarkEditor::slotLabelEdited()
0744 {
0745   // Set the label
0746   QUrl url = m_tree_widget->currentItem()->data(0, QTreeWidgetItem::UserType).toUrl();
0747 
0748   BookmarkPtr bookmark = findBookmark(url);
0749 
0750   if (bookmark)
0751   {
0752     bookmark->setLabel(m_label_edit->userText());
0753   }
0754 
0755   // Add label to completion object
0756   KCompletion *completion = m_label_edit->completionObject();
0757 
0758   if (!m_label_edit->userText().isEmpty())
0759   {
0760     completion->addItem(m_label_edit->userText());
0761   }
0762 }
0763 
0764 
0765 void Smb4KBookmarkEditor::slotLoginEdited()
0766 {
0767   // Set the login
0768   QUrl url = m_tree_widget->currentItem()->data(0, QTreeWidgetItem::UserType).toUrl();
0769 
0770   BookmarkPtr bookmark = findBookmark(url);
0771 
0772   if (bookmark)
0773   {
0774     bookmark->setLogin(m_login_edit->userText());
0775   }
0776 
0777   // Add login to completion object
0778   KCompletion *completion = m_login_edit->completionObject();
0779 
0780   if (!m_login_edit->userText().isEmpty())
0781   {
0782     completion->addItem(m_login_edit->userText());
0783   }
0784 }
0785 
0786 
0787 void Smb4KBookmarkEditor::slotIPEdited()
0788 {
0789   // Set the ip address
0790   QUrl url = m_tree_widget->currentItem()->data(0, QTreeWidgetItem::UserType).toUrl();
0791 
0792   BookmarkPtr bookmark = findBookmark(url);
0793 
0794   if (bookmark)
0795   {
0796     bookmark->setHostIpAddress(m_ip_edit->userText());
0797   }
0798 
0799   // Add login to completion object
0800   KCompletion *completion = m_ip_edit->completionObject();
0801 
0802   if (!m_ip_edit->userText().isEmpty())
0803   {
0804     completion->addItem(m_ip_edit->userText());
0805   }
0806 }
0807 
0808 
0809 void Smb4KBookmarkEditor::slotGroupEdited()
0810 {
0811   //
0812   // Get the URL of the current item.
0813   //
0814   QUrl url = m_tree_widget->currentItem()->data(0, QTreeWidgetItem::UserType).toUrl();
0815   
0816   //
0817   // Return here, if the current item is a group
0818   //
0819   if (url.isEmpty())
0820   {
0821     return;
0822   }
0823   
0824   //
0825   // Set the group name to the bookmark
0826   //
0827   BookmarkPtr bookmark = findBookmark(url);
0828   
0829   if (bookmark)
0830   {
0831     bookmark->setGroupName(m_group_combo->currentText());
0832   }
0833   
0834   //
0835   // Reload the bookmarks (The current item is cleared by this!)
0836   //
0837   loadBookmarks();
0838   
0839   //
0840   // Reset the current item
0841   // 
0842   QTreeWidgetItemIterator it(m_tree_widget);
0843   
0844   while (*it)
0845   {
0846     if ((*it)->data(0, QTreeWidgetItem::UserType).toUrl() == url)
0847     {
0848       m_tree_widget->setCurrentItem(*it);
0849       slotItemClicked(*it, 0);
0850       break;
0851     }
0852     
0853     ++it;
0854   }
0855 
0856   // 
0857   // Add the group to the completion object
0858   // 
0859   KCompletion *completion = m_group_combo->completionObject();
0860 
0861   if (!m_group_combo->currentText().isEmpty())
0862   {
0863     completion->addItem(m_group_combo->currentText());
0864   }
0865 }
0866 
0867 
0868 void Smb4KBookmarkEditor::slotAddGroupTriggered(bool /*checked*/)
0869 {
0870   bool ok = false;
0871   
0872   QString group_name = QInputDialog::getText(this, i18n("Add Group"), i18n("Group name:"), QLineEdit::Normal, QString(), &ok);
0873 
0874   if (ok && !group_name.isEmpty() && m_tree_widget->findItems(group_name, Qt::MatchFixedString|Qt::MatchCaseSensitive, 0).isEmpty())
0875   {
0876     // Create a new group item and add it to the widget
0877     QTreeWidgetItem *group = new QTreeWidgetItem(QTreeWidgetItem::UserType);
0878     group->setIcon(0, KDE::icon("folder-bookmark"));
0879     group->setText(0, group_name);
0880     group->setText((m_tree_widget->columnCount() - 1), QString("00_%1").arg(group_name));
0881     group->setFlags(Qt::ItemIsSelectable|Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::ItemIsDropEnabled) ;
0882     m_tree_widget->addTopLevelItem(group);
0883     m_tree_widget->sortItems((m_tree_widget->columnCount() - 1), Qt::AscendingOrder);
0884 
0885     // Add the group to the combo box
0886     m_group_combo->addItem(group_name);
0887     m_group_combo->completionObject()->addItem(group_name);
0888   }
0889 }
0890 
0891 
0892 void Smb4KBookmarkEditor::slotDeleteTriggered(bool /*checked*/)
0893 {
0894   //
0895   // Remove the bookmarks from the view and the internal list
0896   //
0897   QList<QTreeWidgetItem *> selected = m_tree_widget->selectedItems();
0898   
0899   while (!selected.isEmpty())
0900   {
0901     QTreeWidgetItem *item = selected.takeFirst();    
0902     QUrl url = item->data(0, QTreeWidgetItem::UserType).toUrl();
0903     
0904     QMutableListIterator<BookmarkPtr> it(m_bookmarks);
0905     
0906     while (it.hasNext())
0907     {
0908       BookmarkPtr bookmark = it.next();
0909       
0910       if (bookmark->url() == url)
0911       {
0912         it.remove();
0913         break;
0914       }
0915     }
0916     
0917     delete item;
0918   }
0919 }
0920 
0921 
0922 void Smb4KBookmarkEditor::slotClearTriggered(bool /*checked*/)
0923 {
0924   m_tree_widget->clear();
0925   m_bookmarks.clear();
0926   m_groups.clear();
0927 }
0928 
0929 
0930 void Smb4KBookmarkEditor::slotDialogAccepted()
0931 {
0932   //
0933   // Write the dialog properties to the config file
0934   // 
0935   KConfigGroup group(Smb4KSettings::self()->config(), "BookmarkEditor");
0936   KWindowConfig::saveWindowSize(windowHandle(), group);
0937   group.writeEntry("LabelCompletion", m_label_edit->completionObject()->items());
0938   group.writeEntry("LoginCompletion", m_login_edit->completionObject()->items());
0939   group.writeEntry("IPCompletion", m_ip_edit->completionObject()->items());
0940   group.writeEntry("GroupCompletion", m_group_combo->completionObject()->items());
0941   
0942   //
0943   // Accept the dialog
0944   // 
0945   accept();
0946 }
0947 
0948 
0949 void Smb4KBookmarkEditor::slotDialogRejected()
0950 {
0951   //
0952   // Reject the dialog
0953   // 
0954   reject();
0955 }
0956 
0957 
0958 
0959 void Smb4KBookmarkEditor::slotIconSizeChanged(int group)
0960 {
0961   switch (group)
0962   {
0963     case KIconLoader::Small:
0964     {
0965       int icon_size = KIconLoader::global()->currentSize(KIconLoader::Small);
0966       m_tree_widget->setIconSize(QSize(icon_size, icon_size));
0967       break;
0968     }
0969     default:
0970     {
0971       break;
0972     }
0973   }
0974 }
0975 
0976 
0977 void Smb4KBookmarkEditor::slotAdjust()
0978 {
0979   // Do the necessary adjustments:
0980   QTreeWidgetItemIterator it(m_tree_widget);
0981   while (*it)
0982   {
0983     if (!(*it)->parent())
0984     {
0985       if ((*it)->data(0, QTreeWidgetItem::UserType).toUrl().isEmpty())
0986       {
0987         if ((*it)->childCount() == 0)
0988         {
0989           delete *it;
0990         }
0991       }
0992       else
0993       {
0994         BookmarkPtr bookmark = findBookmark((*it)->data(0, QTreeWidgetItem::UserType).toUrl());
0995       
0996         if (bookmark)
0997         {
0998           bookmark->setGroupName("");
0999         }
1000       }
1001     }
1002     else
1003     {
1004       BookmarkPtr bookmark = findBookmark((*it)->data(0, QTreeWidgetItem::UserType).toUrl());
1005       
1006       if (bookmark)
1007       {
1008         bookmark->setGroupName((*it)->parent()->text(0));
1009       }
1010     }
1011     ++it;
1012   }
1013 }
1014 
1015