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

KDEUI

krecentfilesaction.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002     Copyright (C) 1999 Reginald Stadlbauer <reggie@kde.org>
00003               (C) 1999 Simon Hausmann <hausmann@kde.org>
00004               (C) 2000 Nicolas Hadacek <haadcek@kde.org>
00005               (C) 2000 Kurt Granroth <granroth@kde.org>
00006               (C) 2000 Michael Koch <koch@kde.org>
00007               (C) 2001 Holger Freyther <freyther@kde.org>
00008               (C) 2002 Ellis Whitehead <ellis@kde.org>
00009               (C) 2002 Joseph Wenninger <jowenn@kde.org>
00010               (C) 2003 Andras Mantia <amantia@kde.org>
00011               (C) 2005-2006 Hamish Rodda <rodda@kde.org>
00012 
00013     This library is free software; you can redistribute it and/or
00014     modify it under the terms of the GNU Library General Public
00015     License version 2 as published by the Free Software Foundation.
00016 
00017     This library is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020     Library General Public License for more details.
00021 
00022     You should have received a copy of the GNU Library General Public License
00023     along with this library; see the file COPYING.LIB.  If not, write to
00024     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00025     Boston, MA 02110-1301, USA.
00026 */
00027 
00028 #include "krecentfilesaction.h"
00029 #include "krecentfilesaction_p.h"
00030 
00031 #include <QtCore/QFile>
00032 #ifdef Q_OS_WIN
00033 #include <QtCore/QDir>
00034 #endif
00035 
00036 #include <kconfig.h>
00037 #include <kconfiggroup.h>
00038 #include <kdebug.h>
00039 #include <kicon.h>
00040 #include <klocale.h>
00041 #include <kstandarddirs.h>
00042 
00043 #include "kmenu.h"
00044 
00045 
00046 KRecentFilesAction::KRecentFilesAction(QObject *parent)
00047   : KSelectAction(*new KRecentFilesActionPrivate, parent)
00048 {
00049   Q_D(KRecentFilesAction);
00050   d->init();
00051 }
00052 
00053 KRecentFilesAction::KRecentFilesAction(const QString &text, QObject *parent)
00054   : KSelectAction(*new KRecentFilesActionPrivate, parent)
00055 {
00056   Q_D(KRecentFilesAction);
00057   d->init();
00058 
00059   // Want to keep the ampersands
00060   setText(text);
00061 }
00062 
00063 KRecentFilesAction::KRecentFilesAction(const KIcon &icon, const QString &text, QObject *parent)
00064   : KSelectAction(*new KRecentFilesActionPrivate, parent)
00065 {
00066   Q_D(KRecentFilesAction);
00067   d->init();
00068 
00069   setIcon(icon);
00070   // Want to keep the ampersands
00071   setText(text);
00072 }
00073 
00074 void KRecentFilesActionPrivate::init()
00075 {
00076   Q_Q(KRecentFilesAction);
00077   delete q->menu();
00078   q->setMenu(new KMenu());
00079   q->setToolBarMode(KSelectAction::MenuMode);
00080   m_noEntriesAction=new QAction(i18n("No entries"),q->selectableActionGroup());
00081   m_noEntriesAction->setEnabled(false);
00082   q->KSelectAction::addAction(m_noEntriesAction);
00083   q->connect(q, SIGNAL(triggered(QAction*)), SLOT(_k_urlSelected(QAction*)));
00084 }
00085 
00086 KRecentFilesAction::~KRecentFilesAction()
00087 {
00088 }
00089 
00090 void KRecentFilesActionPrivate::_k_urlSelected( QAction* action )
00091 {
00092     Q_Q(KRecentFilesAction);
00093     emit q->urlSelected(m_urls[action]);
00094 }
00095 
00096 int KRecentFilesAction::maxItems() const
00097 {
00098     Q_D(const KRecentFilesAction);
00099     return d->m_maxItems;
00100 }
00101 
00102 void KRecentFilesAction::setMaxItems( int maxItems )
00103 {
00104     Q_D(KRecentFilesAction);
00105     // set new maxItems
00106     d->m_maxItems = maxItems;
00107 
00108     // remove all excess items
00109     while( selectableActionGroup()->actions().count() > maxItems )
00110         delete removeAction(selectableActionGroup()->actions().last());
00111 }
00112 
00113 void KRecentFilesAction::addUrl( const KUrl& _url, const QString& name )
00114 {
00115     Q_D(KRecentFilesAction);
00121     const KUrl url( _url );
00122 
00123     if ( url.isLocalFile() && KGlobal::dirs()->relativeLocation("tmp", url.path()) != url.path() )
00124        return;
00125     const QString tmpName = name.isEmpty() ?  url.fileName() : name;
00126 #ifdef Q_OS_WIN
00127     const QString file = url.isLocalFile() ? QDir::toNativeSeparators( url.pathOrUrl() ) : url.pathOrUrl();
00128 #else
00129     const QString file = url.pathOrUrl();
00130 #endif
00131 
00132     // remove file if already in list
00133     foreach (QAction* action, selectableActionGroup()->actions())
00134     {
00135       if ( d->m_urls[action].pathOrUrl().endsWith(file) )
00136       {
00137         removeAction(action)->deleteLater();
00138         break;
00139       }
00140     }
00141     // remove oldest item if already maxitems in list
00142     if( d->m_maxItems && selectableActionGroup()->actions().count() == d->m_maxItems )
00143     {
00144         // remove oldest added item
00145         delete removeAction(selectableActionGroup()->actions().first());
00146     }
00147 
00148     if (d->m_noEntriesAction) removeAction(d->m_noEntriesAction)->deleteLater();
00149     // add file to list
00150     const QString title = tmpName + " [" + file + ']';
00151     QAction* action = new QAction(title, selectableActionGroup());
00152     addAction(action, url, tmpName);
00153 }
00154 
00155 void KRecentFilesAction::addAction(QAction* action, const KUrl& url, const QString& name)
00156 {
00157   Q_D(KRecentFilesAction);
00158   //kDebug (129) << "KRecentFilesAction::addAction(" << action << ")";
00159 
00160   action->setActionGroup(selectableActionGroup());
00161 
00162   // Keep in sync with createToolBarWidget()
00163   foreach (QToolButton* button, d->m_buttons)
00164     button->insertAction(button->actions().value(0), action);
00165 
00166   foreach (KComboBox* comboBox, d->m_comboBoxes)
00167     comboBox->insertAction(comboBox->actions().value(0), action);
00168 
00169   menu()->insertAction(menu()->actions().value(0), action);
00170 
00171   d->m_shortNames.insert( action, name );
00172   d->m_urls.insert( action, url );
00173 }
00174 
00175 QAction* KRecentFilesAction::removeAction(QAction* action)
00176 {
00177   Q_D(KRecentFilesAction);
00178   KSelectAction::removeAction( action );
00179 
00180   d->m_shortNames.remove( action );
00181   d->m_urls.remove( action );
00182 
00183   return action;
00184 }
00185 
00186 void KRecentFilesAction::removeUrl( const KUrl& url )
00187 {
00188   Q_D(KRecentFilesAction);
00189   for (QMap<QAction*, KUrl>::ConstIterator it = d->m_urls.constBegin(); it != d->m_urls.constEnd(); ++it)
00190     if (it.value() == url) {
00191       delete removeAction(it.key());
00192       return;
00193     }
00194 }
00195 
00196 KUrl::List KRecentFilesAction::urls() const
00197 {
00198   Q_D(const KRecentFilesAction);
00199   return d->m_urls.values ();
00200 }
00201 
00202 void KRecentFilesAction::clear()
00203 {
00204     Q_D(KRecentFilesAction);
00205     KSelectAction::clear();
00206     d->m_shortNames.clear();
00207     d->m_urls.clear();
00208     if (d->m_noEntriesAction) KSelectAction::removeAction(d->m_noEntriesAction)->deleteLater();
00209     d->m_noEntriesAction=new QAction(i18n("No entries"),selectableActionGroup());
00210     d->m_noEntriesAction->setEnabled(false);
00211     KSelectAction::addAction(d->m_noEntriesAction);
00212 }
00213 
00214 void KRecentFilesAction::loadEntries( const KConfigGroup& _config)
00215 {
00216     Q_D(KRecentFilesAction);
00217     clear();
00218 
00219     QString     key;
00220     QString     value;
00221     QString     nameKey;
00222     QString     nameValue;
00223     QString      title;
00224     KUrl        url;
00225 
00226     KConfigGroup cg = _config;
00227     if ( cg.name().isEmpty())
00228         cg = KConfigGroup(cg.config(),"RecentFiles");
00229 
00230     bool thereAreEntries=false;
00231     // read file list
00232     for( int i = 1 ; i <= d->m_maxItems ; i++ )
00233     {
00234         key = QString( "File%1" ).arg( i );
00235         value = cg.readPathEntry( key, QString() );
00236         if (value.isEmpty()) continue;
00237         url = KUrl( value );
00238 
00239         // Don't restore if file doesn't exist anymore
00240         if (url.isLocalFile() && !QFile::exists(url.path()))
00241           continue;
00242 
00243         // Don't restore where the url is already known (eg. broken config)
00244         if (d->m_urls.values().contains(url))
00245           continue;
00246 
00247 #ifdef Q_OS_WIN
00248         // convert to backslashes
00249         if ( url.isLocalFile() )
00250             value = QDir::toNativeSeparators( value );
00251 #endif
00252 
00253         nameKey = QString( "Name%1" ).arg( i );
00254         nameValue = cg.readPathEntry( nameKey, url.fileName() );
00255         title = nameValue + " [" + value + ']';
00256         if (!value.isNull())
00257         {
00258           thereAreEntries=true;
00259           addAction(new QAction(title, selectableActionGroup()), url, nameValue);
00260         }
00261     }
00262     if (thereAreEntries)
00263     {
00264         if (d->m_noEntriesAction) KSelectAction::removeAction(d->m_noEntriesAction)->deleteLater();
00265     }
00266 }
00267 
00268 void KRecentFilesAction::saveEntries( const KConfigGroup &_cg )
00269 {
00270     Q_D(KRecentFilesAction);
00271     QString     key;
00272     QString     value;
00273     QStringList lst = items();
00274 
00275     KConfigGroup cg = _cg;
00276     if (cg.name().isEmpty())
00277         cg = KConfigGroup(cg.config(),"RecentFiles");
00278 
00279     cg.deleteGroup();
00280 
00281     // write file list
00282     if ( (selectableActionGroup()->actions().count()>1) ||
00283     ( (selectableActionGroup()->actions().count()==1) && (selectableActionGroup()->actions()[0]!=d->m_noEntriesAction) ) )
00284     for ( int i = 1 ; i <= selectableActionGroup()->actions().count() ; i++ )
00285     {
00286         key = QString( "File%1" ).arg( i );
00287         // i - 1 because we started from 1
00288         value = d->m_urls[ selectableActionGroup()->actions()[ i - 1 ] ].pathOrUrl();
00289         cg.writePathEntry( key, value );
00290         key = QString( "Name%1" ).arg( i );
00291         value = d->m_shortNames[ selectableActionGroup()->actions()[ i - 1 ] ];
00292         cg.writePathEntry( key, value );
00293     }
00294 
00295 }
00296 
00297 /* vim: et sw=2 ts=2
00298  */
00299 
00300 #include "krecentfilesaction.moc"

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • 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
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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