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

KIO

  • kio
  • bookmarks
kbookmarkdialog.cc
Go to the documentation of this file.
1 // -*- c-basic-offset:4; indent-tabs-mode:nil -*-
2 // vim: set ts=4 sts=4 sw=4 et:
3 /* This file is part of the KDE libraries
4  Copyright 2007 Daniel Teske <teske@squorn.de>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public 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
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "kbookmarkdialog.h"
22 #include "kbookmarkmanager.h"
23 #include "kbookmarkmenu.h"
24 #include "kbookmarkmenu_p.h"
25 #include <QFormLayout>
26 #include <QLabel>
27 #include <QTreeWidget>
28 #include <QHeaderView>
29 #include <klineedit.h>
30 #include <klocale.h>
31 #include <kiconloader.h>
32 #include <kinputdialog.h>
33 #include <kstandardguiitem.h>
34 
35 
36 KBookmark KBookmarkDialog::editBookmark(const KBookmark & bm)
37 {
38  if(!m_layout)
39  initLayoutPrivate();
40  setButtons( Ok | Cancel );
41  setButtonGuiItem( KDialog::Ok, KGuiItem(i18nc("@action:button", "Update" )) );
42  setCaption( i18nc("@title:window","Bookmark Properties") );
43  m_url->setVisible(!bm.isGroup());
44  m_urlLabel->setVisible(!bm.isGroup());
45  m_bm = bm;
46  m_title->setText(bm.fullText());
47  m_url->setText(bm.url().url());
48  m_comment->setVisible(true);
49  m_commentLabel->setVisible(true);
50  m_comment->setText(bm.description());
51  m_folderTree->setVisible(false);
52 
53  m_mode = EditBookmark;
54  aboutToShow(m_mode);
55 
56  if(exec() == QDialog::Accepted)
57  return m_bm;
58  else
59  return KBookmark();
60 
61 }
62 
63 KBookmark KBookmarkDialog::addBookmark(const QString & title, const KUrl & url, KBookmark parent)
64 {
65  if(!m_layout)
66  initLayoutPrivate();
67  if(parent.isNull())
68  parent = m_mgr->root();
69  setButtons( User1 | Ok | Cancel );
70  setButtonGuiItem( KDialog::Ok, KGuiItem( i18nc("@action:button", "Add" ), "bookmark-new") );
71  setCaption( i18nc("@title:window","Add Bookmark") );
72  setButtonGuiItem( User1, KGuiItem( i18nc("@action:button", "&New Folder..." ), "folder-new") );
73  m_url->setVisible(true);
74  m_urlLabel->setVisible(true);
75  m_title->setText(title);
76  m_url->setText(url.url());
77  m_comment->setText(QString());
78  m_comment->setVisible(true);
79  m_commentLabel->setVisible(true);
80  setParentBookmark(parent);
81  m_folderTree->setVisible(true);
82 
83  m_mode = NewBookmark;
84  aboutToShow(m_mode);
85 
86  if(exec() == QDialog::Accepted)
87  return m_bm;
88  else
89  return KBookmark();
90 }
91 
92 KBookmarkGroup KBookmarkDialog::addBookmarks(const QList<QPair<QString, QString> > & list, const QString & name, KBookmarkGroup parent)
93 {
94  if(!m_layout)
95  initLayoutPrivate();
96  if(parent.isNull())
97  parent = m_mgr->root();
98 
99  m_list = list;
100 
101  setButtons( User1 | Ok | Cancel);
102  setButtonGuiItem( KDialog::Ok, KGuiItem( i18nc("@action:button", "Add" ), "bookmark-new") );
103  setCaption( i18nc("@title:window","Add Bookmarks") );
104  setButtonGuiItem( User1, KGuiItem( i18nc("@action:button", "&New Folder..." ), "folder-new") );
105  m_url->setVisible(false);
106  m_urlLabel->setVisible(false);
107  m_title->setText(name);
108  m_comment->setVisible(true);
109  m_commentLabel->setVisible(true);
110  m_comment->setText(QString());
111  setParentBookmark(parent);
112  m_folderTree->setVisible(true);
113 
114  m_mode = NewMultipleBookmarks;
115  aboutToShow(m_mode);
116 
117  if(exec() == QDialog::Accepted)
118  return m_bm.toGroup();
119  else
120  return KBookmarkGroup();
121 }
122 
123 KBookmarkGroup KBookmarkDialog::selectFolder(KBookmark parent)
124 {
125  if(!m_layout)
126  initLayoutPrivate();
127  if(parent.isNull())
128  parent = m_mgr->root();
129  setButtons( User1 | Ok | Cancel );
130  setButtonGuiItem( KDialog::Ok, KStandardGuiItem::ok() );
131  setButtonGuiItem( User1, KGuiItem( i18nc("@action:button", "&New Folder..." ), "folder-new") );
132  setCaption( i18nc("@title:window","Select Folder"));
133  m_url->setVisible(false);
134  m_urlLabel->setVisible(false);
135  m_title->setVisible(false);
136  m_titleLabel->setVisible(false);
137  m_comment->setVisible(false);
138  m_commentLabel->setVisible(false);
139  setParentBookmark(parent);
140  m_folderTree->setVisible(true);
141 
142  m_mode = SelectFolder;
143  aboutToShow(m_mode);
144 
145  if(exec() == QDialog::Accepted)
146  return m_bm.toGroup();
147  else
148  return KBookmarkGroup();
149 }
150 
151 KBookmarkGroup KBookmarkDialog::createNewFolder(const QString & name, KBookmark parent)
152 {
153  if(!m_layout)
154  initLayoutPrivate();
155  if(parent.isNull())
156  parent = m_mgr->root();
157  setButtons( Ok | Cancel );
158  setButtonGuiItem( KDialog::Ok, KStandardGuiItem::ok() );
159  setCaption( i18nc("@title:window","New Folder"));
160  m_url->setVisible(false);
161  m_urlLabel->setVisible(false);
162  m_comment->setVisible(true);
163  m_commentLabel->setVisible(true);
164  m_comment->setText(QString());
165  m_title->setText(name);
166  setParentBookmark(parent);
167  m_folderTree->setVisible(true);
168 
169  m_mode = NewFolder;
170  aboutToShow(m_mode);
171 
172  if(exec() == QDialog::Accepted)
173  return m_bm.toGroup();
174  else
175  return KBookmarkGroup();
176 }
177 
178 void KBookmarkDialog::setParentBookmark(const KBookmark & bm)
179 {
180  QString address = bm.address();
181  KBookmarkTreeItem * item = static_cast<KBookmarkTreeItem *>(m_folderTree->topLevelItem(0));
182  while(true)
183  {
184  if(item->address() == bm.address())
185  {
186  m_folderTree->setCurrentItem(item);
187  return;
188  }
189  for(int i=0; i<item->childCount(); ++i)
190  {
191  KBookmarkTreeItem * child = static_cast<KBookmarkTreeItem *>(item->child(i));
192  if( KBookmark::commonParent(child->address(), address) == child->address())
193  {
194  item = child;
195  break;
196  }
197  }
198  }
199 }
200 
201 KBookmarkGroup KBookmarkDialog::parentBookmark()
202 {
203  KBookmarkTreeItem *item = dynamic_cast<KBookmarkTreeItem *>(m_folderTree->currentItem());
204  if(!item)
205  return m_mgr->root();
206  const QString &address = item->address();
207  return m_mgr->findByAddress(address).toGroup();
208 }
209 
210 void KBookmarkDialog::slotButtonClicked(int button)
211 {
212  if(button == Ok)
213  {
214  if(m_mode == NewFolder)
215  {
216  KBookmarkGroup parent = parentBookmark();
217  if(m_title->text().isEmpty())
218  m_title->setText("New Folder");
219  m_bm = parent.createNewFolder(m_title->text());
220  m_bm.setDescription(m_comment->text());
221  save(m_mode, m_bm);
222  m_mgr->emitChanged(parent);
223  } else if(m_mode == NewBookmark) {
224  KBookmarkGroup parent = parentBookmark();
225  if(m_title->text().isEmpty())
226  m_title->setText("New Bookmark");
227  m_bm = parent.addBookmark(m_title->text(), KUrl(m_url->text()));
228  m_bm.setDescription(m_comment->text());
229  save(m_mode, m_bm);
230  m_mgr->emitChanged(parent);
231  } else if(m_mode == NewMultipleBookmarks) {
232  KBookmarkGroup parent = parentBookmark();
233  if(m_title->text().isEmpty())
234  m_title->setText("New Folder");
235  m_bm = parent.createNewFolder(m_title->text());
236  m_bm.setDescription(m_comment->text());
237  QList< QPair<QString, QString> >::iterator it, end;
238  end = m_list.end();
239  for(it = m_list.begin(); it!= m_list.end(); ++it)
240  {
241  m_bm.toGroup().addBookmark( (*it).first, KUrl((*it).second));
242  }
243  save(m_mode, m_bm);
244  m_mgr->emitChanged(parent);
245  } else if(m_mode == EditBookmark) {
246  m_bm.setFullText(m_title->text());
247  m_bm.setUrl(KUrl(m_url->text()));
248  m_bm.setDescription(m_comment->text());
249  save(m_mode, m_bm);
250  m_mgr->emitChanged(m_bm.parentGroup());
251  } else if(m_mode == SelectFolder) {
252  m_bm = parentBookmark();
253  save(m_mode, m_bm);
254  }
255  }
256  KDialog::slotButtonClicked(button);
257 }
258 
259 void KBookmarkDialog::save(BookmarkDialogMode , const KBookmark & )
260 {
261 
262 }
263 
264 void KBookmarkDialog::aboutToShow(BookmarkDialogMode mode)
265 {
266  Q_UNUSED(mode);
267 }
268 
269 void KBookmarkDialog::initLayout()
270 {
271  QBoxLayout *vbox = new QVBoxLayout( m_main );
272  vbox->setMargin(0);
273  QFormLayout * form = new QFormLayout();
274  vbox->addLayout(form);
275 
276  form->addRow( m_titleLabel, m_title );
277  form->addRow( m_urlLabel, m_url );
278  form->addRow( m_commentLabel, m_comment );
279 
280  vbox->addWidget(m_folderTree);
281 }
282 
283 
284 void KBookmarkDialog::initLayoutPrivate()
285 {
286  m_main = new QWidget( this );
287  setMainWidget( m_main );
288  connect( this, SIGNAL( user1Clicked() ), SLOT( newFolderButton() ) );
289 
290  m_title = new KLineEdit( m_main );
291  m_title->setMinimumWidth(300);
292  m_titleLabel = new QLabel( i18nc("@label:textbox", "Name:" ), m_main );
293  m_titleLabel->setBuddy( m_title );
294 
295  m_url = new KLineEdit( m_main );
296  m_url->setMinimumWidth(300);
297  m_urlLabel = new QLabel( i18nc("@label:textbox", "Location:" ), m_main );
298  m_urlLabel->setBuddy( m_url );
299 
300  m_comment = new KLineEdit( m_main );
301  m_comment->setMinimumWidth(300);
302  m_commentLabel = new QLabel( i18nc("@label:textbox", "Comment:" ), m_main );
303  m_commentLabel->setBuddy( m_comment );
304 
305  m_folderTree = new QTreeWidget(m_main);
306  m_folderTree->setColumnCount(1);
307  m_folderTree->header()->hide();
308  m_folderTree->setSortingEnabled(false);
309  m_folderTree->setSelectionMode( QTreeWidget::SingleSelection );
310  m_folderTree->setSelectionBehavior( QTreeWidget::SelectRows );
311  m_folderTree->setMinimumSize( 60, 100 );
312  QTreeWidgetItem *root = new KBookmarkTreeItem(m_folderTree);
313  fillGroup( root, m_mgr->root() );
314 
315  initLayout();
316  m_layout = true;
317 }
318 
319 
320 KBookmarkDialog::KBookmarkDialog(KBookmarkManager * mgr, QWidget * parent )
321  : KDialog(parent),
322  m_folderTree(0), m_mgr(mgr), m_layout(false)
323 {
324 
325 }
326 
327 void KBookmarkDialog::newFolderButton()
328 {
329 
330  QString caption = parentBookmark().fullText().isEmpty() ?
331  i18nc("@title:window","Create New Bookmark Folder" ) :
332  i18nc("@title:window","Create New Bookmark Folder in %1" ,
333  parentBookmark().text() );
334  bool ok;
335  QString text = KInputDialog::getText( caption, i18nc("@label:textbox", "New folder:" ), QString(), &ok );
336  if ( !ok )
337  return;
338 
339  KBookmarkGroup group = parentBookmark().createNewFolder(text);
340  if ( !group.isNull() )
341  {
342  KBookmarkGroup parentGroup = group.parentGroup();
343  m_mgr->emitChanged( parentGroup );
344  m_folderTree->clear();
345  QTreeWidgetItem *root = new KBookmarkTreeItem(m_folderTree);
346  fillGroup( root, m_mgr->root() );
347  }
348 }
349 
350 void KBookmarkDialog::fillGroup( QTreeWidgetItem * parentItem, const KBookmarkGroup &group)
351 {
352  for ( KBookmark bk = group.first() ; !bk.isNull() ; bk = group.next(bk) )
353  {
354  if ( bk.isGroup() )
355  {
356  QTreeWidgetItem* item = new KBookmarkTreeItem(parentItem, m_folderTree, bk.toGroup() );
357  fillGroup( item, bk.toGroup() );
358  }
359  }
360 }
361 
362 /********************************************************************/
363 
364 KBookmarkTreeItem::KBookmarkTreeItem(QTreeWidget * tree)
365  : QTreeWidgetItem(tree), m_address("")
366 {
367  setText(0, i18nc("name of the container of all browser bookmarks","Bookmarks"));
368  setIcon(0, SmallIcon("bookmarks"));
369  tree->expandItem(this);
370  tree->setCurrentItem( this );
371  tree->setItemSelected( this, true );
372 }
373 
374 KBookmarkTreeItem::KBookmarkTreeItem(QTreeWidgetItem * parent, QTreeWidget * tree, const KBookmarkGroup &bk)
375  : QTreeWidgetItem(parent)
376 {
377  setIcon(0, SmallIcon(bk.icon()));
378  setText(0, bk.fullText() );
379  tree->expandItem(this);
380  m_address = bk.address();
381 }
382 
383 KBookmarkTreeItem::~KBookmarkTreeItem()
384 {
385 }
386 
387 QString KBookmarkTreeItem::address()
388 {
389  return m_address;
390 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Apr 16 2013 21:04:50 by doxygen 1.8.3.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

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

kdelibs-4.10.2 API Reference

Skip menu "kdelibs-4.10.2 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