File: search-action.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 (272 lines) | stat: -rw-r--r-- 6,468 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
/*
 * 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 "search-action.h"

#include "query.h"
#include "settings.h"

using namespace WhiskerMenu;

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

SearchAction::SearchAction() :
	m_is_regex(false),
	m_show_description(true),
	m_regex(nullptr)
{
	set_icon("folder-saved-search");
	update_text();
}

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

SearchAction::SearchAction(const gchar* name, const gchar* pattern, const gchar* command, bool is_regex) :
	m_name(name ? name : ""),
	m_pattern(pattern ? pattern : ""),
	m_command(command ? command : ""),
	m_is_regex(is_regex),
	m_show_description(true),
	m_regex(nullptr)
{
	set_icon("folder-saved-search");
	update_text();
}

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

SearchAction::~SearchAction()
{
	if (m_regex)
	{
		g_regex_unref(m_regex);
	}
}

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

unsigned int SearchAction::search(const Query& query)
{
	if (m_pattern.empty() || m_command.empty())
	{
		return false;
	}

	m_expanded_command.clear();

	const gchar* haystack = query.raw_query().c_str();
	unsigned int found = !m_is_regex ? match_prefix(haystack) : match_regex(haystack);

	const bool show_description = wm_settings->launcher_show_description && (wm_settings->view_mode != Settings::ViewAsIcons);
	if ((found != UINT_MAX) && (m_show_description != show_description))
	{
		m_show_description = show_description;
		update_text();
	}

	return found;
}

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

unsigned int SearchAction::match_prefix(const gchar* haystack)
{
	if (!g_str_has_prefix(haystack, m_pattern.c_str()))
	{
		return UINT_MAX;
	}

	gchar* trimmed = g_strdup(haystack + m_pattern.length());
	trimmed = g_strstrip(trimmed);

	gchar* uri = nullptr;

	m_expanded_command = m_command;
	std::string::size_type pos = 0, lastpos = m_expanded_command.length() - 1;
	while ((pos = m_expanded_command.find('%', pos)) != std::string::npos)
	{
		if (pos == lastpos)
		{
			break;
		}

		switch (m_expanded_command[pos + 1])
		{
		case 's':
			m_expanded_command.replace(pos, 2, trimmed);
			pos += strlen(trimmed) + 1;
			break;

		case 'S':
			m_expanded_command.replace(pos, 2, haystack);
			pos += strlen(haystack) + 1;
			break;

		case 'u':
			if (!uri)
			{
				uri = g_uri_escape_string(trimmed, nullptr, true);
			}
			m_expanded_command.replace(pos, 2, uri);
			pos += strlen(uri) + 1;
			break;

		case '%':
			m_expanded_command.erase(pos, 1);
			pos += 1;
			break;

		default:
			m_expanded_command.erase(pos, 2);
			break;
		}
	}

	g_free(trimmed);
	g_free(uri);

	return m_pattern.length();
}

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

unsigned int SearchAction::match_regex(const gchar* haystack)
{
	unsigned int found = UINT_MAX;

	if (!m_regex)
	{
		m_regex = g_regex_new(m_pattern.c_str(), G_REGEX_OPTIMIZE, GRegexMatchFlags(0), nullptr);
		if (!m_regex)
		{
			return found;
		}
	}
	GMatchInfo* match = nullptr;
	if (g_regex_match(m_regex, haystack, GRegexMatchFlags(0), &match))
	{
		gchar* expanded = g_match_info_expand_references(match, m_command.c_str(), nullptr);
		if (expanded)
		{
			m_expanded_command = expanded;
			g_free(expanded);
			found = m_pattern.length();
		}
	}
	if (match)
	{
		g_match_info_free(match);
	}

	return found;
}

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

void SearchAction::run(GdkScreen* screen) const
{
	spawn(screen, m_expanded_command.c_str(), nullptr, false, nullptr);
}

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

void SearchAction::set_name(const gchar* name)
{
	if (!name || (m_name == name))
	{
		return;
	}

	m_name = name;
	wm_settings->search_actions.set_modified();

	m_show_description = wm_settings->launcher_show_description && (wm_settings->view_mode != Settings::ViewAsIcons);
	update_text();
}

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

void SearchAction::set_pattern(const gchar* pattern)
{
	if (!pattern || (m_pattern == pattern))
	{
		return;
	}

	m_pattern = pattern;
	wm_settings->search_actions.set_modified();

	if (m_regex)
	{
		g_regex_unref(m_regex);
		m_regex = nullptr;
	}
}

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

void SearchAction::set_command(const gchar* command)
{
	if (!command || (m_command == command))
	{
		return;
	}

	m_command = command;
	wm_settings->search_actions.set_modified();
}

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

void SearchAction::set_is_regex(bool is_regex)
{
	if (m_is_regex == is_regex)
	{
		return;
	}

	m_is_regex = is_regex;
	wm_settings->search_actions.set_modified();
}

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

bool SearchAction::operator==(const SearchAction& action) const
{
	return ((m_pattern == action.m_pattern)
			&& (m_command == action.m_command)
			&& (m_is_regex == action.m_is_regex));
}

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

void SearchAction::update_text()
{
	const gchar* direction = (gtk_widget_get_default_direction() != GTK_TEXT_DIR_RTL) ? "\342\200\216" : "\342\200\217";
	const gchar* description = _("Search Action");
	if (m_show_description)
	{
		set_text(g_markup_printf_escaped("%s<b>%s</b>\n%s%s", direction, m_name.c_str(), direction, description));
	}
	else
	{
		set_text(g_markup_printf_escaped("%s%s", direction, m_name.c_str()));
	}
	set_tooltip(description);
}

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