• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

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 <QGridLayout>
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_folderTree->setVisible(false);
00049 
00050     m_mode = EditBookmark;
00051     aboutToShow(m_mode);
00052 
00053     if(exec() == QDialog::Accepted)
00054         return m_bm;
00055     else
00056         return KBookmark();
00057 
00058 }
00059 
00060 KBookmark KBookmarkDialog::addBookmark(const QString & title, const KUrl & url, KBookmark parent)
00061 {
00062     if(!m_layout)
00063         initLayoutPrivate();
00064     if(parent.isNull())
00065         parent = m_mgr->root();
00066     setButtons( User1 | Ok | Cancel );
00067     setButtonGuiItem( KDialog::Ok,  KGuiItem( i18nc("@action:button", "Add" ), "bookmark-new") );
00068     setCaption( i18nc("@title:window","Add Bookmark") );
00069     setButtonGuiItem( User1, KGuiItem( i18nc("@action:button", "&New Folder..." ), "folder-new") );
00070     m_url->setVisible(true);
00071     m_urlLabel->setVisible(true);
00072     m_title->setText(title);    
00073     m_url->setText(url.url());
00074     setParentBookmark(parent);
00075     m_folderTree->setVisible(true);
00076 
00077     m_mode = NewBookmark;
00078     aboutToShow(m_mode);
00079 
00080     if(exec() == QDialog::Accepted)
00081         return m_bm;
00082     else
00083         return KBookmark();
00084 }
00085 
00086 KBookmarkGroup KBookmarkDialog::addBookmarks(const QList<QPair<QString, QString> > & list, const QString & name, KBookmarkGroup parent)
00087 {
00088     if(!m_layout)
00089         initLayoutPrivate();
00090     if(parent.isNull())
00091         parent = m_mgr->root();
00092 
00093     m_list = list;
00094 
00095     setButtons( User1 | Ok | Cancel);
00096     setButtonGuiItem( KDialog::Ok,  KGuiItem( i18nc("@action:button", "Add" ), "bookmark-new") );
00097     setCaption( i18nc("@title:window","Add Bookmarks") );
00098     setButtonGuiItem( User1, KGuiItem( i18nc("@action:button", "&New Folder..." ), "folder-new") );
00099     m_url->setVisible(false);
00100     m_urlLabel->setVisible(false);
00101     m_title->setText(name);
00102     setParentBookmark(parent);
00103     m_folderTree->setVisible(true);
00104 
00105     m_mode = NewMultipleBookmarks;
00106     aboutToShow(m_mode);
00107     
00108     if(exec() == QDialog::Accepted)
00109         return m_bm.toGroup();
00110     else
00111         return KBookmarkGroup();
00112 }
00113 
00114 KBookmarkGroup KBookmarkDialog::selectFolder(KBookmark parent)
00115 {
00116     if(!m_layout)
00117         initLayoutPrivate();
00118     if(parent.isNull())
00119         parent = m_mgr->root();
00120     setButtons( User1 | Ok | Cancel );
00121     setButtonGuiItem( KDialog::Ok, KStandardGuiItem::ok() );
00122     setButtonGuiItem( User1, KGuiItem( i18nc("@action:button", "&New Folder..." ), "folder-new") );
00123     setCaption( i18nc("@title:window","Select Folder"));
00124     m_url->setVisible(false);
00125     m_urlLabel->setVisible(false);
00126     m_title->setVisible(false);
00127     m_titleLabel->setVisible(false);
00128     setParentBookmark(parent);
00129     m_folderTree->setVisible(true);
00130 
00131     m_mode = SelectFolder;
00132     aboutToShow(m_mode);
00133 
00134     if(exec() == QDialog::Accepted)
00135         return m_bm.toGroup();
00136     else
00137         return KBookmarkGroup();
00138 }
00139 
00140 KBookmarkGroup KBookmarkDialog::createNewFolder(const QString & name, KBookmark parent)
00141 {
00142     if(!m_layout)
00143         initLayoutPrivate();
00144     if(parent.isNull())
00145         parent = m_mgr->root();
00146     setButtons( Ok | Cancel );
00147     setButtonGuiItem( KDialog::Ok, KStandardGuiItem::ok() );
00148     setCaption( i18nc("@title:window","New Folder"));
00149     m_url->setVisible(false);
00150     m_urlLabel->setVisible(false);
00151     m_title->setText(name);
00152     setParentBookmark(parent);
00153     m_folderTree->setVisible(true);
00154 
00155     m_mode = NewFolder;
00156     aboutToShow(m_mode);
00157 
00158     if(exec() == QDialog::Accepted)
00159         return m_bm.toGroup();
00160     else
00161         return KBookmarkGroup();
00162 }
00163 
00164 void KBookmarkDialog::setParentBookmark(const KBookmark & bm)
00165 {
00166     QString address = bm.address();
00167     KBookmarkTreeItem * item = static_cast<KBookmarkTreeItem *>(m_folderTree->topLevelItem(0));
00168     while(true)
00169     {
00170         if(item->address() == bm.address())
00171         {
00172             m_folderTree->setCurrentItem(item);
00173             return;
00174         }
00175         for(int i=0; i<item->childCount(); ++i)
00176         {
00177             KBookmarkTreeItem * child = static_cast<KBookmarkTreeItem *>(item->child(i));
00178             if( KBookmark::commonParent(child->address(), address) == child->address())
00179             {
00180                 item = child;
00181                 break;
00182             }
00183         }
00184     }
00185 }
00186 
00187 KBookmarkGroup KBookmarkDialog::parentBookmark()
00188 {
00189     KBookmarkTreeItem *item = dynamic_cast<KBookmarkTreeItem *>(m_folderTree->currentItem());
00190     if(!item)
00191         return m_mgr->root();
00192     const QString &address = item->address();
00193     return m_mgr->findByAddress(address).toGroup();
00194 }
00195 
00196 void KBookmarkDialog::slotButtonClicked(int button)
00197 {
00198     if(button == Ok)
00199     {
00200         if(m_mode == NewFolder)
00201         {
00202             KBookmarkGroup parent = parentBookmark();
00203             if(m_title->text().isEmpty())
00204                 m_title->setText("New Folder");
00205             m_bm = parent.createNewFolder(m_title->text());
00206             save(m_mode, m_bm);
00207             m_mgr->emitChanged(parent);
00208         } else if(m_mode == NewBookmark) {
00209             KBookmarkGroup parent = parentBookmark();
00210             if(m_title->text().isEmpty())
00211                 m_title->setText("New Bookmark");
00212             m_bm = parent.addBookmark(m_title->text(), KUrl(m_url->text()));
00213             save(m_mode, m_bm);
00214             m_mgr->emitChanged(parent);
00215         } else if(m_mode == NewMultipleBookmarks) {
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             QList< QPair<QString, QString> >::iterator  it, end;
00221             end = m_list.end();
00222             for(it = m_list.begin(); it!= m_list.end(); ++it)
00223             {
00224                 m_bm.toGroup().addBookmark( (*it).first, KUrl((*it).second));
00225             }
00226             save(m_mode, m_bm);
00227             m_mgr->emitChanged(parent);
00228         } else if(m_mode == EditBookmark) {
00229             m_bm.setFullText(m_title->text());
00230             m_bm.setUrl(KUrl(m_url->text()));
00231             save(m_mode, m_bm);
00232             m_mgr->emitChanged(m_bm.parentGroup());
00233         } else if(m_mode == SelectFolder) {
00234             m_bm = parentBookmark();
00235             save(m_mode, m_bm);
00236         }
00237     }
00238     KDialog::slotButtonClicked(button);
00239 }
00240 
00241 void KBookmarkDialog::save(BookmarkDialogMode , const KBookmark & )
00242 {
00243 
00244 }
00245 
00246 void KBookmarkDialog::aboutToShow(BookmarkDialogMode mode)
00247 {
00248     Q_UNUSED(mode);
00249 }
00250 
00251 void KBookmarkDialog::initLayout()
00252 {
00253     QBoxLayout *vbox = new QVBoxLayout( m_main );
00254     QGridLayout * grid = new QGridLayout();
00255     vbox->addLayout(grid);
00256 
00257     grid->addWidget( m_title, 0, 1 );
00258     grid->addWidget( m_titleLabel, 0, 0 );
00259 
00260     grid->addWidget( m_url, 1, 1 );
00261     grid->addWidget( m_urlLabel, 1, 0 );
00262 
00263     vbox->addWidget(m_folderTree);
00264 }
00265 
00266 
00267 void KBookmarkDialog::initLayoutPrivate()
00268 {
00269     m_main = new QWidget( this );
00270     setMainWidget( m_main );
00271     connect( this, SIGNAL( user1Clicked() ), SLOT( newFolderButton() ) );
00272 
00273     m_title = new KLineEdit( m_main );
00274     m_titleLabel = new QLabel( i18nc("@label:textbox", "Name:" ), m_main );
00275     m_titleLabel->setBuddy( m_title );
00276 
00277     m_url = new KLineEdit( m_main );
00278     m_urlLabel = new QLabel( i18nc("@label:textbox", "Location:" ), m_main );
00279     m_urlLabel->setBuddy( m_url );
00280 
00281     m_folderTree = new QTreeWidget(m_main);
00282     m_folderTree->setColumnCount(1);
00283     m_folderTree->header()->hide();
00284     m_folderTree->setSortingEnabled(false);
00285     m_folderTree->setSelectionMode( QTreeWidget::SingleSelection );
00286     m_folderTree->setSelectionBehavior( QTreeWidget::SelectRows );
00287     m_folderTree->setMinimumSize( 60, 100 );
00288     QTreeWidgetItem *root = new KBookmarkTreeItem(m_folderTree);    
00289     fillGroup( root, m_mgr->root() );
00290 
00291 
00292     initLayout();
00293     m_layout = true;
00294 }
00295 
00296 
00297 KBookmarkDialog::KBookmarkDialog(KBookmarkManager * mgr, QWidget * parent )
00298   : KDialog(parent),
00299     m_folderTree(0), m_mgr(mgr), m_layout(false)
00300 {
00301  
00302 }
00303 
00304 void KBookmarkDialog::newFolderButton()
00305 {
00306 
00307     QString caption = parentBookmark().fullText().isEmpty() ?
00308                       i18nc("@title:window","Create New Bookmark Folder" ) :
00309                       i18nc("@title:window","Create New Bookmark Folder in %1" ,
00310                         parentBookmark().text() );
00311     bool ok;
00312     QString text = KInputDialog::getText( caption, i18nc("@label:textbox", "New folder:" ), QString(), &ok );
00313     if ( !ok )
00314         return;
00315 
00316     KBookmarkGroup group = parentBookmark().createNewFolder(text);
00317     if ( !group.isNull() )
00318     {
00319         KBookmarkGroup parentGroup = group.parentGroup();
00320         m_mgr->emitChanged( parentGroup );
00321         m_folderTree->clear();
00322         QTreeWidgetItem *root = new KBookmarkTreeItem(m_folderTree);
00323         fillGroup( root, m_mgr->root() );
00324     }
00325 }
00326 
00327 void KBookmarkDialog::fillGroup( QTreeWidgetItem * parentItem, const KBookmarkGroup &group)
00328 {
00329   for ( KBookmark bk = group.first() ; !bk.isNull() ; bk = group.next(bk) )
00330   {
00331     if ( bk.isGroup() )
00332     {
00333       QTreeWidgetItem* item = new KBookmarkTreeItem(parentItem, m_folderTree, bk.toGroup() );
00334       fillGroup( item, bk.toGroup() );
00335     }
00336   }
00337 }
00338 
00339 /********************************************************************/
00340 
00341 KBookmarkTreeItem::KBookmarkTreeItem(QTreeWidget * tree)
00342     : QTreeWidgetItem(tree), m_address("")
00343 {
00344     setText(0, i18n("Bookmarks"));
00345     setIcon(0, SmallIcon("bookmarks"));
00346     tree->expandItem(this);
00347     tree->setCurrentItem( this );
00348     tree->setItemSelected( this, true );
00349 }
00350 
00351 KBookmarkTreeItem::KBookmarkTreeItem(QTreeWidgetItem * parent, QTreeWidget * tree, const KBookmarkGroup &bk)
00352     : QTreeWidgetItem(parent)
00353 {
00354     setIcon(0, SmallIcon(bk.icon()));
00355     setText(0, bk.fullText() );
00356     tree->expandItem(this);
00357     m_address = bk.address();
00358 }
00359 
00360 KBookmarkTreeItem::~KBookmarkTreeItem()
00361 {
00362 }
00363 
00364 QString KBookmarkTreeItem::address()
00365 {
00366     return m_address;
00367 }

KIO

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal