summaryrefslogtreecommitdiffstats
path: root/src/recentfilesaction.cpp
blob: ac38d6bdb72130d5c7f38fdb875c0a01258a8092 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/***************************************************************************
 *   Copyright (C) 2005 by David Saxton                                    *
 *   [email protected]                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "recentfilesaction.h"

#include <kconfig.h>
#include <kpopupmenu.h>
#include <kstandarddirs.h>
#include <kurl.h>

RecentFilesAction::RecentFilesAction( const QString & configGroupName, const QString& text, const QObject* receiver, const char* slot, QObject* parent, const char* name )
	: KSelectAction( text, 0/*pix*/, parent, name )
{
	m_configGroupName = configGroupName;
	m_maxItems = 10;
	
	m_popup = new KPopupMenu;
	connect(m_popup, SIGNAL(aboutToShow()), this, SLOT(menuAboutToShow()));
	connect(m_popup, SIGNAL(activated(int)), this, SLOT(menuItemActivated(int)));
	connect( this, SIGNAL( activated( const QString& ) ),
			 this, SLOT( itemSelected( const QString& ) ) );

	setMenuAccelsEnabled( false );
	
	if ( receiver )
		connect( this, SIGNAL(urlSelected(const KURL &)), receiver, slot );
}


RecentFilesAction::~RecentFilesAction()
{
	delete m_popup;
}

void RecentFilesAction::addURL( const KURL& url )
{
	if ( url.isLocalFile() && !KGlobal::dirs()->relativeLocation("tmp", url.path()).startsWith("/"))
		return;
	
	QString file;
	if ( url.isLocalFile() && url.ref().isNull() && url.query().isNull() )
		file = url.path();
	else
		file = url.prettyURL();
	
	QStringList lst = items();

    // remove file if already in list
	lst.remove( file );

    // remove last item if already maxitems in list
	if( lst.count() == m_maxItems )
	{
        // remove last item
		lst.remove( lst.last() );
	}

    // add file to list
	lst.prepend( file );
	setItems( lst );
	
	saveEntries();
}


void RecentFilesAction::loadEntries()
{
	KConfig * config = KGlobal::config();
	
	QString     key;
	QString     value;
	QString     oldGroup;
	QStringList lst;

	oldGroup = config->group();

	config->setGroup( m_configGroupName );

    // read file list
	for( unsigned int i = 1 ; i <= m_maxItems ; i++ )
	{
		key = QString( "File%1" ).arg( i );
		value = config->readPathEntry( key );

		if (!value.isNull())
			lst.append( value );
	}

    // set file
	setItems( lst );

	config->setGroup( oldGroup );
}

void RecentFilesAction::saveEntries()
{
	KConfig * config = KGlobal::config();
	
	QString     key;
	QString     value;
	QString     oldGroup;
	QStringList lst = items();

	oldGroup = config->group();

	config->deleteGroup( m_configGroupName, true );
	config->setGroup( m_configGroupName );

    // write file list
	for( unsigned int i = 1 ; i <= lst.count() ; i++ )
	{
		key = QString( "File%1" ).arg( i );
		value = lst[ i - 1 ];
		config->writePathEntry( key, value );
	}

	config->setGroup( oldGroup );
	
	config->sync();
}

void RecentFilesAction::itemSelected( const QString& text )
{
	emit urlSelected( KURL( text ) );
}

void RecentFilesAction::menuItemActivated( int id )
{
	emit urlSelected( KURL(m_popup->text(id)) );
}

void RecentFilesAction::menuAboutToShow()
{
	KPopupMenu *menu = m_popup;
	menu->clear();
	QStringList list = items();
	QStringList::iterator end = list.end();
	for ( QStringList::Iterator it = list.begin(); it != end; ++it )
		menu->insertItem(*it);
}

void RecentFilesAction::slotClicked()
{
	KAction::slotActivated();
}

void RecentFilesAction::slotActivated(const QString& text)
{
	KSelectAction::slotActivated(text);
}


void RecentFilesAction::slotActivated(int id)
{
	KSelectAction::slotActivated(id);
}


void RecentFilesAction::slotActivated()
{
	emit activated( currentItem() );
	emit activated( currentText() );
}


#include "recentfilesaction.moc"