File: applications-page.cpp

package info (click to toggle)
xfce4-whiskermenu-plugin 2.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,616 kB
  • sloc: cpp: 8,368; ansic: 25; makefile: 9
file content (451 lines) | stat: -rw-r--r-- 10,899 bytes parent folder | download | duplicates (2)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
 * Copyright (C) 2013-2021 Graeme Gott <graeme@gottcode.org>
 *
 * This library 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.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "applications-page.h"

#include "category.h"
#include "category-button.h"
#include "launcher.h"
#include "launcher-view.h"
#include "settings.h"
#include "slot.h"
#include "window.h"

#include <algorithm>

using namespace WhiskerMenu;

//-----------------------------------------------------------------------------

ApplicationsPage::ApplicationsPage(Window* window) :
	Page(window, "applications-other", _("All Applications")),
	m_garcon_menu(nullptr),
	m_garcon_settings_menu(nullptr),
	m_status(LoadStatus::Invalid)
{
	garcon_set_environment_xdg(GARCON_ENVIRONMENT_XFCE);

	const decltype(m_categories.size()) index = 0;
	connect(get_button()->get_widget(), "toggled",
		[this](GtkToggleButton* button)
		{
			show_category(button, index);
		});
}

//-----------------------------------------------------------------------------

ApplicationsPage::~ApplicationsPage()
{
	clear();
}

//-----------------------------------------------------------------------------

GtkTreeModel* ApplicationsPage::create_launcher_model(StringList& desktop_ids) const
{
	// Create new model for treeview
	GtkListStore* store = gtk_list_store_new(
			LauncherView::N_COLUMNS,
			G_TYPE_ICON,
			G_TYPE_STRING,
			G_TYPE_STRING,
			G_TYPE_POINTER);

	// Fetch menu items or remove them from list if missing
	for (int i = 0; i < desktop_ids.size(); ++i)
	{
		const std::string& desktop_id = desktop_ids[i];
		if (desktop_id.empty())
		{
			continue;
		}

		Launcher* launcher = find(desktop_id);
		if (launcher)
		{
			gtk_list_store_insert_with_values(
					store, nullptr, G_MAXINT,
					LauncherView::COLUMN_ICON, launcher->get_icon(),
					LauncherView::COLUMN_TEXT, launcher->get_text(),
					LauncherView::COLUMN_TOOLTIP, launcher->get_tooltip(),
					LauncherView::COLUMN_LAUNCHER, launcher,
					-1);
		}
		else
		{
			desktop_ids.erase(i);
			--i;
		}
	}

	return GTK_TREE_MODEL(store);
}

//-----------------------------------------------------------------------------

Launcher* ApplicationsPage::find(const std::string& desktop_id) const
{
	auto i = m_items.find(desktop_id);
	return (i != m_items.end()) ? i->second : nullptr;
}

//-----------------------------------------------------------------------------

std::vector<Launcher*> ApplicationsPage::find_all() const
{
	std::vector<Launcher*> launchers;
	launchers.reserve(m_items.size());
	for (const auto& i : m_items)
	{
		launchers.push_back(i.second);
	}
	std::sort(launchers.begin(), launchers.end(), &Element::less_than);
	return launchers;
}

//-----------------------------------------------------------------------------

void ApplicationsPage::show_category(GtkToggleButton* togglebutton, std::vector<Category*>::size_type index)
{
	// Only apply filter for active button and valid category
	if (!gtk_toggle_button_get_active(togglebutton) || m_categories.empty())
	{
		return;
	}

	// Apply filter
	Category* category = m_categories[index];
	get_view()->unset_model();
	get_view()->set_fixed_height_mode(!category->has_separators());
	get_view()->set_model(category->get_model());
}

//-----------------------------------------------------------------------------

void ApplicationsPage::invalidate()
{
	if (m_status == LoadStatus::Done)
	{
		m_status = LoadStatus::Invalid;
	}
	else if (m_status == LoadStatus::Loading)
	{
		m_status = LoadStatus::ReloadRequired;
	}
}

//-----------------------------------------------------------------------------

bool ApplicationsPage::load()
{
	// Check if already loaded
	if (m_status == LoadStatus::Done)
	{
		return true;
	}
	// Check if currently loading
	else if (m_status != LoadStatus::Invalid)
	{
		return false;
	}
	m_status = LoadStatus::Loading;

	// Load menu
	clear();

	// Load contents in thread if possible
	GTask* task = g_task_new(nullptr, nullptr,
		+[](GObject*, GAsyncResult*, gpointer user_data)
		{
			static_cast<ApplicationsPage*>(user_data)->load_contents();
		},
		this);
	g_task_set_task_data(task, this, nullptr);
	g_task_run_in_thread(task,
		+[](GTask* task, gpointer, gpointer task_data, GCancellable*)
		{
			static_cast<ApplicationsPage*>(task_data)->load_garcon_menu();
			g_task_return_boolean(task, true);
		});
	g_object_unref(task);

	return false;
}

//-----------------------------------------------------------------------------

void ApplicationsPage::reload_category_icon_size()
{
	for (auto category : m_categories)
	{
		category->get_button()->reload_icon_size();
	}
}

//-----------------------------------------------------------------------------

void ApplicationsPage::clear()
{
	// Free categories
	for (auto category : m_categories)
	{
		delete category;
	}
	m_categories.clear();

	// Free menu items
	get_window()->unset_items();
	get_view()->unset_model();

	for (const auto& i : m_items)
	{
		delete i.second;
	}
	m_items.clear();

	// Free menu
	if (G_LIKELY(m_garcon_menu))
	{
		g_object_unref(m_garcon_menu);
		m_garcon_menu = nullptr;
	}

	// Free settings menu
	if (G_LIKELY(m_garcon_settings_menu))
	{
		g_object_unref(m_garcon_settings_menu);
		m_garcon_settings_menu = nullptr;
	}
}

//-----------------------------------------------------------------------------

void ApplicationsPage::load_garcon_menu()
{
	// Create menu
	if (wm_settings->custom_menu_file.empty())
	{
		m_garcon_menu = garcon_menu_new_applications();
	}
	else
	{
		m_garcon_menu = garcon_menu_new_for_path(wm_settings->custom_menu_file);
	}

	// Load menu
	if (m_garcon_menu && !garcon_menu_load(m_garcon_menu, nullptr, nullptr))
	{
		g_object_unref(m_garcon_menu);
		m_garcon_menu = nullptr;
	}

	if (!m_garcon_menu)
	{
		return;
	}

	connect(m_garcon_menu, "reload-required",
		[this](GarconMenu*)
		{
			invalidate();
		});

	load_menu(m_garcon_menu, nullptr, wm_settings->view_mode == Settings::ViewAsTree);

	// Create settings menu
	gchar* path = xfce_resource_lookup(XFCE_RESOURCE_CONFIG, "menus/xfce-settings-manager.menu");
	m_garcon_settings_menu = garcon_menu_new_for_path(path ? path : SETTINGS_MENUFILE);
	g_free(path);

	if (m_garcon_settings_menu)
	{
		connect(m_garcon_settings_menu, "reload-required",
			[this](GarconMenu*)
			{
				invalidate();
			});
	}

	// Load settings menu
	if (m_garcon_settings_menu && garcon_menu_load(m_garcon_settings_menu, nullptr, nullptr))
	{
		Category* category = new Category(nullptr);
		load_menu(m_garcon_settings_menu, category, false);
		delete category;
	}

	// Sort items and categories
	if (wm_settings->view_mode != Settings::ViewAsTree)
	{
		for (auto category : m_categories)
		{
			category->sort();
		}
	}
	if (wm_settings->sort_categories)
	{
		std::sort(m_categories.begin(), m_categories.end(), &Element::less_than);
	}

	// Create all items category
	Category* category = new Category(nullptr);
	category->set_button(get_button());
	category->append_items(find_all());
	m_categories.insert(m_categories.begin(), category);
}

//-----------------------------------------------------------------------------

void ApplicationsPage::load_contents()
{
	if (!m_garcon_menu)
	{
		get_window()->set_loaded();

		m_status = LoadStatus::Invalid;

		return;
	}

	// Set all applications category
	get_view()->set_fixed_height_mode(true);
	get_view()->set_model(m_categories.front()->get_model());

	// Add buttons for categories
	std::vector<CategoryButton*> category_buttons;
	const auto size = m_categories.size();
	for (decltype(m_categories.size()) i = 1; i < size; ++i)
	{
		CategoryButton* category_button = m_categories[i]->get_button();
		connect(category_button->get_widget(), "toggled",
			[this, i](GtkToggleButton* button)
			{
				show_category(button, i);
			});
		category_buttons.push_back(category_button);
	}

	// Add category buttons to window
	get_window()->set_categories(category_buttons);

	// Update menu items of other panels
	get_window()->set_items();
	get_window()->set_loaded();

	m_status = (m_status == LoadStatus::Loading) ? LoadStatus::Done : LoadStatus::Invalid;
}

//-----------------------------------------------------------------------------

bool ApplicationsPage::load_menu(GarconMenu* menu, Category* parent_category, bool load_hierarchy)
{
	bool has_children = false;

	// Add menu elements
	GList* elements = garcon_menu_get_elements(menu);
	for (GList* li = elements; li; li = li->next)
	{
		// Add menu item
		if (GARCON_IS_MENU_ITEM(li->data))
		{
			GarconMenuItem* menuitem = GARCON_MENU_ITEM(li->data);

			// Listen for changes
			connect(menuitem, "changed",
				[this](GarconMenuItem*)
				{
					invalidate();
				});

			// Skip hidden items
			if (!garcon_menu_element_get_visible(GARCON_MENU_ELEMENT(menuitem)))
			{
				continue;
			}

			// Create launcher
			std::string desktop_id(garcon_menu_item_get_desktop_id(menuitem));
			auto iter = m_items.find(desktop_id);
			if (iter == m_items.end())
			{
				iter = m_items.emplace(std::move(desktop_id), new Launcher(menuitem)).first;
			}

			// Add launcher to current category
			if (parent_category)
			{
				parent_category->append_item(iter->second);
			}

			has_children = true;
		}
		// Add separator
		else if (GARCON_IS_MENU_SEPARATOR(li->data) && load_hierarchy && parent_category)
		{
			parent_category->append_separator();
		}
		// Add submenu
		else if (GARCON_IS_MENU(li->data))
		{
			GarconMenu* submenu = GARCON_MENU(li->data);

			// Skip hidden categories
			GarconMenuDirectory* directory = garcon_menu_get_directory(submenu);
			if (directory && !garcon_menu_directory_get_visible(directory))
			{
				continue;
			}

			// Create category
			Category* category = nullptr;
			if (!load_hierarchy && parent_category)
			{
				category = parent_category;
			}
			else
			{
				category = new Category(submenu);
			}

			// Populate category
			if (load_menu(submenu, category, load_hierarchy))
			{
				if (!parent_category)
				{
					m_categories.push_back(category);
				}
				else if (category != parent_category)
				{
					parent_category->append_category(category);
				}

				has_children = true;
			}
			// Remove empty categories
			else if (category != parent_category)
			{
				delete category;
			}
		}
	}
	g_list_free(elements);

	return has_children;
}

//-----------------------------------------------------------------------------