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 = q->menu()->addAction(i18n("No Entries")); 00081 m_noEntriesAction->setEnabled(false); 00082 clearSeparator = q->menu()->addSeparator(); 00083 clearSeparator->setVisible(false); 00084 clearAction = q->menu()->addAction(i18n("Clear List"), q, SLOT(clear())); 00085 clearAction->setVisible(false); 00086 q->setEnabled(false); 00087 q->connect(q, SIGNAL(triggered(QAction*)), SLOT(_k_urlSelected(QAction*))); 00088 } 00089 00090 KRecentFilesAction::~KRecentFilesAction() 00091 { 00092 } 00093 00094 void KRecentFilesActionPrivate::_k_urlSelected( QAction* action ) 00095 { 00096 Q_Q(KRecentFilesAction); 00097 emit q->urlSelected(m_urls[action]); 00098 } 00099 00100 int KRecentFilesAction::maxItems() const 00101 { 00102 Q_D(const KRecentFilesAction); 00103 return d->m_maxItems; 00104 } 00105 00106 void KRecentFilesAction::setMaxItems( int maxItems ) 00107 { 00108 Q_D(KRecentFilesAction); 00109 // set new maxItems 00110 d->m_maxItems = maxItems; 00111 00112 // remove all excess items 00113 while( selectableActionGroup()->actions().count() > maxItems ) 00114 delete removeAction(selectableActionGroup()->actions().last()); 00115 } 00116 00117 void KRecentFilesAction::addUrl( const KUrl& _url, const QString& name ) 00118 { 00119 Q_D(KRecentFilesAction); 00125 const KUrl url( _url ); 00126 00127 if ( url.isLocalFile() && KGlobal::dirs()->relativeLocation("tmp", url.toLocalFile()) != url.toLocalFile() ) 00128 return; 00129 const QString tmpName = name.isEmpty() ? url.fileName() : name; 00130 #ifdef Q_OS_WIN 00131 const QString file = url.isLocalFile() ? QDir::toNativeSeparators( url.pathOrUrl() ) : url.pathOrUrl(); 00132 #else 00133 const QString file = url.pathOrUrl(); 00134 #endif 00135 00136 // remove file if already in list 00137 foreach (QAction* action, selectableActionGroup()->actions()) 00138 { 00139 if ( d->m_urls[action].pathOrUrl().endsWith(file) ) 00140 { 00141 removeAction(action)->deleteLater(); 00142 break; 00143 } 00144 } 00145 // remove oldest item if already maxitems in list 00146 if( d->m_maxItems && selectableActionGroup()->actions().count() == d->m_maxItems ) 00147 { 00148 // remove oldest added item 00149 delete removeAction(selectableActionGroup()->actions().first()); 00150 } 00151 00152 d->m_noEntriesAction->setVisible(false); 00153 d->clearSeparator->setVisible(true); 00154 d->clearAction->setVisible(true); 00155 setEnabled(true); 00156 // add file to list 00157 const QString title = tmpName + " [" + file + ']'; 00158 QAction* action = new QAction(title, selectableActionGroup()); 00159 addAction(action, url, tmpName); 00160 } 00161 00162 void KRecentFilesAction::addAction(QAction* action, const KUrl& url, const QString& name) 00163 { 00164 Q_D(KRecentFilesAction); 00165 //kDebug (129) << "KRecentFilesAction::addAction(" << action << ")"; 00166 00167 action->setActionGroup(selectableActionGroup()); 00168 00169 // Keep in sync with createToolBarWidget() 00170 foreach (QToolButton* button, d->m_buttons) 00171 button->insertAction(button->actions().value(0), action); 00172 00173 foreach (KComboBox* comboBox, d->m_comboBoxes) 00174 comboBox->insertAction(comboBox->actions().value(0), action); 00175 00176 menu()->insertAction(menu()->actions().value(0), action); 00177 00178 d->m_shortNames.insert( action, name ); 00179 d->m_urls.insert( action, url ); 00180 } 00181 00182 QAction* KRecentFilesAction::removeAction(QAction* action) 00183 { 00184 Q_D(KRecentFilesAction); 00185 KSelectAction::removeAction( action ); 00186 00187 d->m_shortNames.remove( action ); 00188 d->m_urls.remove( action ); 00189 00190 return action; 00191 } 00192 00193 void KRecentFilesAction::removeUrl( const KUrl& url ) 00194 { 00195 Q_D(KRecentFilesAction); 00196 for (QMap<QAction*, KUrl>::ConstIterator it = d->m_urls.constBegin(); it != d->m_urls.constEnd(); ++it) 00197 if (it.value() == url) { 00198 delete removeAction(it.key()); 00199 return; 00200 } 00201 } 00202 00203 KUrl::List KRecentFilesAction::urls() const 00204 { 00205 Q_D(const KRecentFilesAction); 00206 return d->m_urls.values (); 00207 } 00208 00209 void KRecentFilesAction::clear() 00210 { 00211 clearEntries(); 00212 emit recentListCleared(); 00213 } 00214 00215 void KRecentFilesAction::clearEntries() 00216 { 00217 Q_D(KRecentFilesAction); 00218 KSelectAction::clear(); 00219 d->m_shortNames.clear(); 00220 d->m_urls.clear(); 00221 d->m_noEntriesAction->setVisible(true); 00222 d->clearSeparator->setVisible(false); 00223 d->clearAction->setVisible(false); 00224 setEnabled(false); 00225 } 00226 00227 void KRecentFilesAction::loadEntries( const KConfigGroup& _config) 00228 { 00229 Q_D(KRecentFilesAction); 00230 clearEntries(); 00231 00232 QString key; 00233 QString value; 00234 QString nameKey; 00235 QString nameValue; 00236 QString title; 00237 KUrl url; 00238 00239 KConfigGroup cg = _config; 00240 if ( cg.name().isEmpty()) 00241 cg = KConfigGroup(cg.config(),"RecentFiles"); 00242 00243 bool thereAreEntries=false; 00244 // read file list 00245 for( int i = 1 ; i <= d->m_maxItems ; i++ ) 00246 { 00247 key = QString( "File%1" ).arg( i ); 00248 value = cg.readPathEntry( key, QString() ); 00249 if (value.isEmpty()) continue; 00250 url = KUrl( value ); 00251 00252 // Don't restore if file doesn't exist anymore 00253 if (url.isLocalFile() && !QFile::exists(url.toLocalFile())) 00254 continue; 00255 00256 // Don't restore where the url is already known (eg. broken config) 00257 if (d->m_urls.values().contains(url)) 00258 continue; 00259 00260 #ifdef Q_OS_WIN 00261 // convert to backslashes 00262 if ( url.isLocalFile() ) 00263 value = QDir::toNativeSeparators( value ); 00264 #endif 00265 00266 nameKey = QString( "Name%1" ).arg( i ); 00267 nameValue = cg.readPathEntry( nameKey, url.fileName() ); 00268 title = nameValue + " [" + value + ']'; 00269 if (!value.isNull()) 00270 { 00271 thereAreEntries=true; 00272 addAction(new QAction(title, selectableActionGroup()), url, nameValue); 00273 } 00274 } 00275 if (thereAreEntries) 00276 { 00277 d->m_noEntriesAction->setVisible(false); 00278 d->clearSeparator->setVisible(true); 00279 d->clearAction->setVisible(true); 00280 setEnabled(true); 00281 } 00282 } 00283 00284 void KRecentFilesAction::saveEntries( const KConfigGroup &_cg ) 00285 { 00286 Q_D(KRecentFilesAction); 00287 QString key; 00288 QString value; 00289 QStringList lst = items(); 00290 00291 KConfigGroup cg = _cg; 00292 if (cg.name().isEmpty()) 00293 cg = KConfigGroup(cg.config(),"RecentFiles"); 00294 00295 cg.deleteGroup(); 00296 00297 // write file list 00298 for ( int i = 1 ; i <= selectableActionGroup()->actions().count() ; i++ ) 00299 { 00300 key = QString( "File%1" ).arg( i ); 00301 // i - 1 because we started from 1 00302 value = d->m_urls[ selectableActionGroup()->actions()[ i - 1 ] ].pathOrUrl(); 00303 cg.writePathEntry( key, value ); 00304 key = QString( "Name%1" ).arg( i ); 00305 value = d->m_shortNames[ selectableActionGroup()->actions()[ i - 1 ] ]; 00306 cg.writePathEntry( key, value ); 00307 } 00308 00309 } 00310 00311 /* vim: et sw=2 ts=2 00312 */ 00313 00314 #include "krecentfilesaction.moc"
KDE 4.6 API Reference