KIO
kbookmarkdialog.cc
Go to the documentation of this file.
00001 // -*- c-basic-offset:4; indent-tabs-mode:nil -*- 00002 // vim: set ts=4 sts=4 sw=4 et: 00003 /* This file is part of the KDE libraries 00004 Copyright 2007 Daniel Teske <teske@squorn.de> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License version 2 as published by the Free Software Foundation. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "kbookmarkdialog.h" 00022 #include "kbookmarkmanager.h" 00023 #include "kbookmarkmenu.h" 00024 #include "kbookmarkmenu_p.h" 00025 #include <QFormLayout> 00026 #include <QLabel> 00027 #include <QTreeWidget> 00028 #include <QHeaderView> 00029 #include <klineedit.h> 00030 #include <klocale.h> 00031 #include <kiconloader.h> 00032 #include <kinputdialog.h> 00033 #include <kstandardguiitem.h> 00034 00035 00036 KBookmark KBookmarkDialog::editBookmark(const KBookmark & bm) 00037 { 00038 if(!m_layout) 00039 initLayoutPrivate(); 00040 setButtons( Ok | Cancel ); 00041 setButtonGuiItem( KDialog::Ok, KGuiItem(i18nc("@action:button", "Update" )) ); 00042 setCaption( i18nc("@title:window","Bookmark Properties") ); 00043 m_url->setVisible(!bm.isGroup()); 00044 m_urlLabel->setVisible(!bm.isGroup()); 00045 m_bm = bm; 00046 m_title->setText(bm.fullText()); 00047 m_url->setText(bm.url().url()); 00048 m_comment->setVisible(true); 00049 m_commentLabel->setVisible(true); 00050 m_comment->setText(bm.description()); 00051 m_folderTree->setVisible(false); 00052 00053 m_mode = EditBookmark; 00054 aboutToShow(m_mode); 00055 00056 if(exec() == QDialog::Accepted) 00057 return m_bm; 00058 else 00059 return KBookmark(); 00060 00061 } 00062 00063 KBookmark KBookmarkDialog::addBookmark(const QString & title, const KUrl & url, KBookmark parent) 00064 { 00065 if(!m_layout) 00066 initLayoutPrivate(); 00067 if(parent.isNull()) 00068 parent = m_mgr->root(); 00069 setButtons( User1 | Ok | Cancel ); 00070 setButtonGuiItem( KDialog::Ok, KGuiItem( i18nc("@action:button", "Add" ), "bookmark-new") ); 00071 setCaption( i18nc("@title:window","Add Bookmark") ); 00072 setButtonGuiItem( User1, KGuiItem( i18nc("@action:button", "&New Folder..." ), "folder-new") ); 00073 m_url->setVisible(true); 00074 m_urlLabel->setVisible(true); 00075 m_title->setText(title); 00076 m_url->setText(url.url()); 00077 m_comment->setText(QString()); 00078 m_comment->setVisible(true); 00079 m_commentLabel->setVisible(true); 00080 setParentBookmark(parent); 00081 m_folderTree->setVisible(true); 00082 00083 m_mode = NewBookmark; 00084 aboutToShow(m_mode); 00085 00086 if(exec() == QDialog::Accepted) 00087 return m_bm; 00088 else 00089 return KBookmark(); 00090 } 00091 00092 KBookmarkGroup KBookmarkDialog::addBookmarks(const QList<QPair<QString, QString> > & list, const QString & name, KBookmarkGroup parent) 00093 { 00094 if(!m_layout) 00095 initLayoutPrivate(); 00096 if(parent.isNull()) 00097 parent = m_mgr->root(); 00098 00099 m_list = list; 00100 00101 setButtons( User1 | Ok | Cancel); 00102 setButtonGuiItem( KDialog::Ok, KGuiItem( i18nc("@action:button", "Add" ), "bookmark-new") ); 00103 setCaption( i18nc("@title:window","Add Bookmarks") ); 00104 setButtonGuiItem( User1, KGuiItem( i18nc("@action:button", "&New Folder..." ), "folder-new") ); 00105 m_url->setVisible(false); 00106 m_urlLabel->setVisible(false); 00107 m_title->setText(name); 00108 m_comment->setVisible(true); 00109 m_commentLabel->setVisible(true); 00110 m_comment->setText(QString()); 00111 setParentBookmark(parent); 00112 m_folderTree->setVisible(true); 00113 00114 m_mode = NewMultipleBookmarks; 00115 aboutToShow(m_mode); 00116 00117 if(exec() == QDialog::Accepted) 00118 return m_bm.toGroup(); 00119 else 00120 return KBookmarkGroup(); 00121 } 00122 00123 KBookmarkGroup KBookmarkDialog::selectFolder(KBookmark parent) 00124 { 00125 if(!m_layout) 00126 initLayoutPrivate(); 00127 if(parent.isNull()) 00128 parent = m_mgr->root(); 00129 setButtons( User1 | Ok | Cancel ); 00130 setButtonGuiItem( KDialog::Ok, KStandardGuiItem::ok() ); 00131 setButtonGuiItem( User1, KGuiItem( i18nc("@action:button", "&New Folder..." ), "folder-new") ); 00132 setCaption( i18nc("@title:window","Select Folder")); 00133 m_url->setVisible(false); 00134 m_urlLabel->setVisible(false); 00135 m_title->setVisible(false); 00136 m_titleLabel->setVisible(false); 00137 m_comment->setVisible(false); 00138 m_commentLabel->setVisible(false); 00139 setParentBookmark(parent); 00140 m_folderTree->setVisible(true); 00141 00142 m_mode = SelectFolder; 00143 aboutToShow(m_mode); 00144 00145 if(exec() == QDialog::Accepted) 00146 return m_bm.toGroup(); 00147 else 00148 return KBookmarkGroup(); 00149 } 00150 00151 KBookmarkGroup KBookmarkDialog::createNewFolder(const QString & name, KBookmark parent) 00152 { 00153 if(!m_layout) 00154 initLayoutPrivate(); 00155 if(parent.isNull()) 00156 parent = m_mgr->root(); 00157 setButtons( Ok | Cancel ); 00158 setButtonGuiItem( KDialog::Ok, KStandardGuiItem::ok() ); 00159 setCaption( i18nc("@title:window","New Folder")); 00160 m_url->setVisible(false); 00161 m_urlLabel->setVisible(false); 00162 m_comment->setVisible(true); 00163 m_commentLabel->setVisible(true); 00164 m_comment->setText(QString()); 00165 m_title->setText(name); 00166 setParentBookmark(parent); 00167 m_folderTree->setVisible(true); 00168 00169 m_mode = NewFolder; 00170 aboutToShow(m_mode); 00171 00172 if(exec() == QDialog::Accepted) 00173 return m_bm.toGroup(); 00174 else 00175 return KBookmarkGroup(); 00176 } 00177 00178 void KBookmarkDialog::setParentBookmark(const KBookmark & bm) 00179 { 00180 QString address = bm.address(); 00181 KBookmarkTreeItem * item = static_cast<KBookmarkTreeItem *>(m_folderTree->topLevelItem(0)); 00182 while(true) 00183 { 00184 if(item->address() == bm.address()) 00185 { 00186 m_folderTree->setCurrentItem(item); 00187 return; 00188 } 00189 for(int i=0; i<item->childCount(); ++i) 00190 { 00191 KBookmarkTreeItem * child = static_cast<KBookmarkTreeItem *>(item->child(i)); 00192 if( KBookmark::commonParent(child->address(), address) == child->address()) 00193 { 00194 item = child; 00195 break; 00196 } 00197 } 00198 } 00199 } 00200 00201 KBookmarkGroup KBookmarkDialog::parentBookmark() 00202 { 00203 KBookmarkTreeItem *item = dynamic_cast<KBookmarkTreeItem *>(m_folderTree->currentItem()); 00204 if(!item) 00205 return m_mgr->root(); 00206 const QString &address = item->address(); 00207 return m_mgr->findByAddress(address).toGroup(); 00208 } 00209 00210 void KBookmarkDialog::slotButtonClicked(int button) 00211 { 00212 if(button == Ok) 00213 { 00214 if(m_mode == NewFolder) 00215 { 00216 KBookmarkGroup parent = parentBookmark(); 00217 if(m_title->text().isEmpty()) 00218 m_title->setText("New Folder"); 00219 m_bm = parent.createNewFolder(m_title->text()); 00220 m_bm.setDescription(m_comment->text()); 00221 save(m_mode, m_bm); 00222 m_mgr->emitChanged(parent); 00223 } else if(m_mode == NewBookmark) { 00224 KBookmarkGroup parent = parentBookmark(); 00225 if(m_title->text().isEmpty()) 00226 m_title->setText("New Bookmark"); 00227 m_bm = parent.addBookmark(m_title->text(), KUrl(m_url->text())); 00228 m_bm.setDescription(m_comment->text()); 00229 save(m_mode, m_bm); 00230 m_mgr->emitChanged(parent); 00231 } else if(m_mode == NewMultipleBookmarks) { 00232 KBookmarkGroup parent = parentBookmark(); 00233 if(m_title->text().isEmpty()) 00234 m_title->setText("New Folder"); 00235 m_bm = parent.createNewFolder(m_title->text()); 00236 m_bm.setDescription(m_comment->text()); 00237 QList< QPair<QString, QString> >::iterator it, end; 00238 end = m_list.end(); 00239 for(it = m_list.begin(); it!= m_list.end(); ++it) 00240 { 00241 m_bm.toGroup().addBookmark( (*it).first, KUrl((*it).second)); 00242 } 00243 save(m_mode, m_bm); 00244 m_mgr->emitChanged(parent); 00245 } else if(m_mode == EditBookmark) { 00246 m_bm.setFullText(m_title->text()); 00247 m_bm.setUrl(KUrl(m_url->text())); 00248 m_bm.setDescription(m_comment->text()); 00249 save(m_mode, m_bm); 00250 m_mgr->emitChanged(m_bm.parentGroup()); 00251 } else if(m_mode == SelectFolder) { 00252 m_bm = parentBookmark(); 00253 save(m_mode, m_bm); 00254 } 00255 } 00256 KDialog::slotButtonClicked(button); 00257 } 00258 00259 void KBookmarkDialog::save(BookmarkDialogMode , const KBookmark & ) 00260 { 00261 00262 } 00263 00264 void KBookmarkDialog::aboutToShow(BookmarkDialogMode mode) 00265 { 00266 Q_UNUSED(mode); 00267 } 00268 00269 void KBookmarkDialog::initLayout() 00270 { 00271 QBoxLayout *vbox = new QVBoxLayout( m_main ); 00272 vbox->setMargin(0); 00273 QFormLayout * form = new QFormLayout(); 00274 vbox->addLayout(form); 00275 00276 form->addRow( m_titleLabel, m_title ); 00277 form->addRow( m_urlLabel, m_url ); 00278 form->addRow( m_commentLabel, m_comment ); 00279 00280 vbox->addWidget(m_folderTree); 00281 } 00282 00283 00284 void KBookmarkDialog::initLayoutPrivate() 00285 { 00286 m_main = new QWidget( this ); 00287 setMainWidget( m_main ); 00288 connect( this, SIGNAL( user1Clicked() ), SLOT( newFolderButton() ) ); 00289 00290 m_title = new KLineEdit( m_main ); 00291 m_title->setMinimumWidth(300); 00292 m_titleLabel = new QLabel( i18nc("@label:textbox", "Name:" ), m_main ); 00293 m_titleLabel->setBuddy( m_title ); 00294 00295 m_url = new KLineEdit( m_main ); 00296 m_url->setMinimumWidth(300); 00297 m_urlLabel = new QLabel( i18nc("@label:textbox", "Location:" ), m_main ); 00298 m_urlLabel->setBuddy( m_url ); 00299 00300 m_comment = new KLineEdit( m_main ); 00301 m_comment->setMinimumWidth(300); 00302 m_commentLabel = new QLabel( i18nc("@label:textbox", "Comment:" ), m_main ); 00303 m_commentLabel->setBuddy( m_comment ); 00304 00305 m_folderTree = new QTreeWidget(m_main); 00306 m_folderTree->setColumnCount(1); 00307 m_folderTree->header()->hide(); 00308 m_folderTree->setSortingEnabled(false); 00309 m_folderTree->setSelectionMode( QTreeWidget::SingleSelection ); 00310 m_folderTree->setSelectionBehavior( QTreeWidget::SelectRows ); 00311 m_folderTree->setMinimumSize( 60, 100 ); 00312 QTreeWidgetItem *root = new KBookmarkTreeItem(m_folderTree); 00313 fillGroup( root, m_mgr->root() ); 00314 00315 initLayout(); 00316 m_layout = true; 00317 } 00318 00319 00320 KBookmarkDialog::KBookmarkDialog(KBookmarkManager * mgr, QWidget * parent ) 00321 : KDialog(parent), 00322 m_folderTree(0), m_mgr(mgr), m_layout(false) 00323 { 00324 00325 } 00326 00327 void KBookmarkDialog::newFolderButton() 00328 { 00329 00330 QString caption = parentBookmark().fullText().isEmpty() ? 00331 i18nc("@title:window","Create New Bookmark Folder" ) : 00332 i18nc("@title:window","Create New Bookmark Folder in %1" , 00333 parentBookmark().text() ); 00334 bool ok; 00335 QString text = KInputDialog::getText( caption, i18nc("@label:textbox", "New folder:" ), QString(), &ok ); 00336 if ( !ok ) 00337 return; 00338 00339 KBookmarkGroup group = parentBookmark().createNewFolder(text); 00340 if ( !group.isNull() ) 00341 { 00342 KBookmarkGroup parentGroup = group.parentGroup(); 00343 m_mgr->emitChanged( parentGroup ); 00344 m_folderTree->clear(); 00345 QTreeWidgetItem *root = new KBookmarkTreeItem(m_folderTree); 00346 fillGroup( root, m_mgr->root() ); 00347 } 00348 } 00349 00350 void KBookmarkDialog::fillGroup( QTreeWidgetItem * parentItem, const KBookmarkGroup &group) 00351 { 00352 for ( KBookmark bk = group.first() ; !bk.isNull() ; bk = group.next(bk) ) 00353 { 00354 if ( bk.isGroup() ) 00355 { 00356 QTreeWidgetItem* item = new KBookmarkTreeItem(parentItem, m_folderTree, bk.toGroup() ); 00357 fillGroup( item, bk.toGroup() ); 00358 } 00359 } 00360 } 00361 00362 /********************************************************************/ 00363 00364 KBookmarkTreeItem::KBookmarkTreeItem(QTreeWidget * tree) 00365 : QTreeWidgetItem(tree), m_address("") 00366 { 00367 setText(0, i18nc("name of the container of all browser bookmarks","Bookmarks")); 00368 setIcon(0, SmallIcon("bookmarks")); 00369 tree->expandItem(this); 00370 tree->setCurrentItem( this ); 00371 tree->setItemSelected( this, true ); 00372 } 00373 00374 KBookmarkTreeItem::KBookmarkTreeItem(QTreeWidgetItem * parent, QTreeWidget * tree, const KBookmarkGroup &bk) 00375 : QTreeWidgetItem(parent) 00376 { 00377 setIcon(0, SmallIcon(bk.icon())); 00378 setText(0, bk.fullText() ); 00379 tree->expandItem(this); 00380 m_address = bk.address(); 00381 } 00382 00383 KBookmarkTreeItem::~KBookmarkTreeItem() 00384 { 00385 } 00386 00387 QString KBookmarkTreeItem::address() 00388 { 00389 return m_address; 00390 }
KDE 4.6 API Reference