summaryrefslogtreecommitdiffstats
path: root/kicker/menuext/recentdocs/recentdocsmenu.cpp
blob: 566543f45899494faee9eeca97771825efa515b1 (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
/*****************************************************************

Copyright (c) 1996-2000 the kicker authors. See file AUTHORS.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************/

#include <tqdragobject.h>
#include <tqstring.h>
#include <tqstringlist.h>

#include <kglobal.h>
#include <kiconloader.h>
#include <kmimetype.h>
#include <klocale.h>
#include <kdesktopfile.h>
#include <kglobalsettings.h>
#include <tdeapplication.h>
#include <kurldrag.h>
#include <tderecentdocument.h>

#include "recentdocsmenu.h"

K_EXPORT_KICKER_MENUEXT(recentdocs, RecentDocsMenu)

RecentDocsMenu::RecentDocsMenu(TQWidget *parent, const char *name,
                               const TQStringList &/*args*/)
    : KPanelMenu(TDERecentDocument::recentDocumentDirectory(), parent, name)
{
}

RecentDocsMenu::~RecentDocsMenu()
{
}

void RecentDocsMenu::initialize() {
	if (initialized()) clear();

	insertItem(SmallIconSet("history_clear"), i18n("Clear History"), this, TQT_SLOT(slotClearHistory()));
	insertSeparator();

	_fileList = TDERecentDocument::recentDocuments();

	if (_fileList.isEmpty()) {
		insertItem(i18n("No Entries"), 0);
		setItemEnabled(0, false);
		return;
	}

	int id = 0;
	char alreadyPresentInMenu;
	TQStringList previousEntries;
	for (TQStringList::ConstIterator it = _fileList.begin(); it != _fileList.end(); ++it) {
		KDesktopFile f(*it, true /* read only */);

		// Make sure this entry is not already present in the menu
		alreadyPresentInMenu = 0;
		for ( TQStringList::Iterator previt = previousEntries.begin(); previt != previousEntries.end(); ++previt ) {
			if (TQString::localeAwareCompare(*previt, f.readName().replace('&', TQString::fromAscii("&&") )) == 0) {
				alreadyPresentInMenu = 1;
			}
		}

		if (alreadyPresentInMenu == 0) {
			// Add item to menu
			insertItem(SmallIconSet(f.readIcon()), f.readName().replace('&', TQString::fromAscii("&&") ), id++);

			// Append to duplicate checking list
			previousEntries.append(f.readName().replace('&', TQString::fromAscii("&&") ));
		}
	}

    setInitialized(true);
}

void RecentDocsMenu::slotClearHistory() {
    TDERecentDocument::clear();
    reinitialize();
}

void RecentDocsMenu::slotExec(int id) {
	if (id >= 0) {
		kapp->propagateSessionManager();
		KURL u;
		u.setPath(_fileList[id]);
		KDEDesktopMimeType::run(u, true);
	}
}

void RecentDocsMenu::mousePressEvent(TQMouseEvent* e) {
	_mouseDown = e->pos();
	TQPopupMenu::mousePressEvent(e);
}

void RecentDocsMenu::mouseMoveEvent(TQMouseEvent* e) {
	KPanelMenu::mouseMoveEvent(e);

	if (!(e->state() & Qt::LeftButton))
		return;

	if (!TQT_TQRECT_OBJECT(rect()).contains(_mouseDown))
		return;

	int dragLength = (e->pos() - _mouseDown).manhattanLength();

	if (dragLength <= TDEGlobalSettings::dndEventDelay())
		return;  // ignore it

	int id = idAt(_mouseDown);

	// Don't drag 'manual' items.
	if (id < 0)
		return;

	KDesktopFile f(_fileList[id], true /* read only */);

	KURL url ( f.readURL() );

	if (url.isEmpty()) // What are we to do ?
		return;

	KURL::List lst;
	lst.append(url);

	KURLDrag* d = new KURLDrag(lst, this);
	d->setPixmap(SmallIcon(f.readIcon()));
	d->dragCopy();
	close();
}

void RecentDocsMenu::slotAboutToShow()
{
    reinitialize();
    KPanelMenu::slotAboutToShow();
}

#include "recentdocsmenu.moc"