KDEUI
krecentfilesaction.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
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
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
00106 d->m_maxItems = maxItems;
00107
00108
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
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
00142 if( d->m_maxItems && selectableActionGroup()->actions().count() == d->m_maxItems )
00143 {
00144
00145 delete removeAction(selectableActionGroup()->actions().first());
00146 }
00147
00148 if (d->m_noEntriesAction) removeAction(d->m_noEntriesAction)->deleteLater();
00149
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
00159
00160 action->setActionGroup(selectableActionGroup());
00161
00162
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
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
00240 if (url.isLocalFile() && !QFile::exists(url.path()))
00241 continue;
00242
00243
00244 if (d->m_urls.values().contains(url))
00245 continue;
00246
00247 #ifdef Q_OS_WIN
00248
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
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
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
00298
00299
00300 #include "krecentfilesaction.moc"