[go: up one dir, main page]

Menu

[12172e]: / src / gnome.cc  Maximize  Restore  History

Download this file

221 lines (177 with data), 5.7 kB

  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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
* IceWM
*
* Copyright (C) 1998-2001 Marko Macek
*
* Changes:
*
* 2000/10/20 mathias.hasselmann@gmx.de
* * read .order files
* * icons for submenus
* * clean up
* 2000/09/19 mathias.hasselmann@gmx.de
* * localized submenus
*/
#include "config.h"
#ifdef CONFIG_GNOME_MENUS
#include "default.h"
#include "ylib.h"
#include "yapp.h"
#include "sysdep.h"
#include "base.h"
#include <dirent.h>
#include "gnomeapps.h"
YPixmap* GnomeMenu::folder_icon = 0;
DGnomeDesktopEntry::DGnomeDesktopEntry(const char *name, YIcon *icon, GnomeDesktopEntry *dentry):
DObject(name, icon)
{
fEntry = dentry;
}
DGnomeDesktopEntry::~DGnomeDesktopEntry() {
gnome_desktop_entry_free(fEntry);
}
void DGnomeDesktopEntry::open() {
XSync(app->display(), False);
gnome_desktop_entry_launch(fEntry);
}
GnomeMenu::GnomeMenu(YWindow *parent,
const char *path): ObjectMenu(parent)
{
fPath = newstr(path);
fModTime = 0;
}
GnomeMenu::~GnomeMenu() {
delete fPath; fPath = 0;
}
void GnomeMenu::updatePopup() {
struct stat sb;
if (stat(fPath, &sb) != 0)
removeAll();
else if (sb.st_mtime > fModTime) {
fModTime = sb.st_mtime;
removeAll();
populateMenu(this);
}
}
void GnomeMenu::createToplevel(ObjectMenu *menu, const char *path) {
GnomeMenu *gmenu = new GnomeMenu(0, path);
if (gmenu != 0) {
gmenu->populateMenu(menu);
delete gmenu;
}
}
void GnomeMenu::createSubmenu(ObjectMenu *menu, const char *path,
const char *name, YPixmap *icon) {
GnomeMenu *gmenu = new GnomeMenu(0, path);
if (gmenu != 0) {
YMenuItem *item = menu->addSubmenu(name, 0, gmenu);
if (icon && item) item->setPixmap(icon);
}
}
void GnomeMenu::populateMenu(ObjectMenu *target) {
const int firstEntry = target->itemCount();
#ifdef LITE
if (folder_icon == 0)
#ifdef CONFIG_IMLIB
if (gnomeFolderIcon) {
char *icon_path = gnome_pixmap_file("gnome-folder.png");
if (icon_path != NULL)
folder_icon = new YPixmap(icon_path, ICON_SMALL, ICON_SMALL);
g_free(icon_path);
} else {
#endif
YIcon *icon = getIcon("folder");
if (icon) folder_icon = icon->small();
#ifdef CONFIG_IMLIB
}
#endif
#endif
const int plen = strlen(fPath);
char *opath = new char[plen + sizeof(".order")];
if (opath) {
strcpy(opath, fPath);
strcpy(opath + plen, ".order");
FILE *order = fopen(opath, "r");
if (order != 0) {
char oentry[100];
while (fgets(oentry, sizeof (oentry), order)) {
const int oend = strlen(oentry) - 1;
if (oend > 0 && oentry[oend] == '\n')
oentry[oend] = '\0';
addEntry(oentry, plen, target, firstEntry);
}
fclose(order);
}
delete opath;
}
DIR *dir = opendir(fPath);
if (dir != 0) {
struct dirent *file;
while ((file = readdir(dir)) != NULL) {
if (*file->d_name != '.')
addEntry(file->d_name, plen, target, firstEntry, false);
}
closedir(dir);
}
}
void GnomeMenu::addEntry(const char *name, const int plen, ObjectMenu *target,
const int firstItem, const bool firstRun) {
const int nlen = (plen == 0 || fPath[plen - 1] != '/')
? plen + 1 + strlen(name)
: plen + strlen(name);
char *npath = new char[nlen + 1];
if (npath) {
strcpy(npath, fPath);
if (plen == 0 || npath[plen - 1] != '/') {
npath[plen] = '/';
strcpy(npath + plen + 1, name);
} else
strcpy(npath + plen, name);
struct stat sb;
const bool isDir = (!stat(npath, &sb) && S_ISDIR(sb.st_mode));
GnomeDesktopEntry *dentry;
if (isDir) {
YMenu *sub = new GnomeMenu(0, npath);
if (sub) {
char *epath = new char[nlen + sizeof("/.directory")];
strcpy(epath, npath);
strcpy(epath + nlen, "/.directory");
dentry = gnome_desktop_entry_load(epath);
const char *tname = (dentry ? dentry->name : name);
if (firstRun || !target->findName(tname, firstItem)) {
YMenuItem *item = target->addSubmenu(tname, 0, sub);
if (item) {
#ifdef CONFIG_IMLIB
YPixmap *icon =
(gnomeFolderIcon && dentry && dentry->icon
? new YPixmap(dentry->icon, ICON_SMALL, ICON_SMALL)
: folder_icon);
if (icon) item->setPixmap(icon);
#else
if (folder_icon) item->setPixmap(folder_icon);
#endif
}
}
gnome_desktop_entry_free(dentry);
delete epath;
}
} else if ((dentry = gnome_desktop_entry_load(npath)) != NULL &&
(firstRun || !target->findName(dentry->name, firstItem))) {
DGnomeDesktopEntry *gde =
new DGnomeDesktopEntry(dentry->name, 0, dentry);
if (gde) {
YMenuItem *item = new DObjectMenuItem(gde);
#ifdef CONFIG_IMLIB
if (dentry->icon) {
YPixmap *icon =
new YPixmap(dentry->icon, ICON_SMALL, ICON_SMALL);
if (icon) item->setPixmap(icon);
}
#endif
target->add(item);
}
}
delete npath;
}
}
#endif