1 /*
2  * This file is part of brisk-menu.
3  *
4  * Copyright © 2017-2020 Brisk Menu Developers
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11 
12 #define _GNU_SOURCE
13 
14 #include "util.h"
15 
16 BRISK_BEGIN_PEDANTIC
17 #include "item.h"
18 BRISK_END_PEDANTIC
19 
G_DEFINE_TYPE(BriskItem,brisk_item,G_TYPE_INITIALLY_UNOWNED)20 G_DEFINE_TYPE(BriskItem, brisk_item, G_TYPE_INITIALLY_UNOWNED)
21 
22 /**
23  * brisk_item_dispose:
24  *
25  * Clean up a BriskItem instance
26  */
27 static void brisk_item_dispose(GObject *obj)
28 {
29         G_OBJECT_CLASS(brisk_item_parent_class)->dispose(obj);
30 }
31 
32 /**
33  * brisk_item_class_init:
34  *
35  * Handle class initialisation
36  */
brisk_item_class_init(BriskItemClass * klazz)37 static void brisk_item_class_init(BriskItemClass *klazz)
38 {
39         GObjectClass *obj_class = G_OBJECT_CLASS(klazz);
40 
41         /* gobject vtable hookup */
42         obj_class->dispose = brisk_item_dispose;
43 }
44 
45 /**
46  * brisk_item_init:
47  *
48  * Handle construction of the BriskItem
49  */
brisk_item_init(__brisk_unused__ BriskItem * self)50 static void brisk_item_init(__brisk_unused__ BriskItem *self)
51 {
52 }
53 
54 /**
55  * brisk_item_get_id:
56  *
57  * Returns the unique ID for this item within the backend
58  * @note This string belongs to the backend, and must not be freed by the caller
59  */
brisk_item_get_id(BriskItem * item)60 const gchar *brisk_item_get_id(BriskItem *item)
61 {
62         g_assert(item != NULL);
63         BriskItemClass *klazz = BRISK_ITEM_GET_CLASS(item);
64         g_assert(klazz->get_id != NULL);
65         return klazz->get_id(item);
66 }
67 
68 /**
69  * brisk_item_get_name:
70  *
71  * Returns the item name used when display the item in the menu
72  * @note This string belongs to the item, and must not be freed by the caller
73  */
brisk_item_get_name(BriskItem * item)74 const gchar *brisk_item_get_name(BriskItem *item)
75 {
76         g_assert(item != NULL);
77         BriskItemClass *klazz = BRISK_ITEM_GET_CLASS(item);
78         g_assert(klazz->get_name != NULL);
79         return klazz->get_name(item);
80 }
81 
82 /**
83  * brisk_item_get_display_name:
84  *
85  * Returns the so-called "display name" (alternative name) for the entry
86  * @note This string belongs to the item, and must not be freed by the caller
87  */
brisk_item_get_display_name(BriskItem * item)88 const gchar *brisk_item_get_display_name(BriskItem *item)
89 {
90         g_assert(item != NULL);
91         BriskItemClass *klazz = BRISK_ITEM_GET_CLASS(item);
92         g_assert(klazz->get_display_name != NULL);
93         return klazz->get_display_name(item);
94 }
95 
96 /**
97  * brisk_item_get_summary:
98  *
99  * Returns the summary used when display the item in the menu
100  * @note This string belongs to the item, and must not be freed by the caller
101  */
brisk_item_get_summary(BriskItem * item)102 const gchar *brisk_item_get_summary(BriskItem *item)
103 {
104         g_assert(item != NULL);
105         BriskItemClass *klazz = BRISK_ITEM_GET_CLASS(item);
106         g_assert(klazz->get_summary != NULL);
107         return klazz->get_summary(item);
108 }
109 
110 /**
111  * brisk_item_get_icon:
112  *
113  * Returns the icon used to display this item in the menu
114  */
brisk_item_get_icon(BriskItem * item)115 const GIcon *brisk_item_get_icon(BriskItem *item)
116 {
117         g_assert(item != NULL);
118         BriskItemClass *klazz = BRISK_ITEM_GET_CLASS(item);
119         if (!klazz->get_icon) {
120                 return NULL;
121         }
122         return klazz->get_icon(item);
123 }
124 
125 /**
126  * brisk_item_get_backend_id:
127  *
128  * Return the ID of the owning backend
129  */
brisk_item_get_backend_id(BriskItem * item)130 const gchar *brisk_item_get_backend_id(BriskItem *item)
131 {
132         g_assert(item != NULL);
133         BriskItemClass *klazz = BRISK_ITEM_GET_CLASS(item);
134         g_assert(klazz->get_backend_id != NULL);
135         return klazz->get_backend_id(item);
136 }
137 
138 /**
139  * brisk_item_matches_search:
140  *
141  * Returns true if the item matches the given search term
142  */
brisk_item_matches_search(BriskItem * item,gchar * term)143 gboolean brisk_item_matches_search(BriskItem *item, gchar *term)
144 {
145         g_assert(item != NULL);
146         BriskItemClass *klazz = BRISK_ITEM_GET_CLASS(item);
147         g_return_val_if_fail(klazz->matches_search != NULL, FALSE);
148         return klazz->matches_search(item, term);
149 }
150 
151 /**
152  * brisk_item_launch:
153  *
154  * Attempt to launch the item
155  */
brisk_item_launch(BriskItem * item,GAppLaunchContext * context)156 gboolean brisk_item_launch(BriskItem *item, GAppLaunchContext *context)
157 {
158         g_assert(item != NULL);
159         BriskItemClass *klazz = BRISK_ITEM_GET_CLASS(item);
160         g_return_val_if_fail(klazz->launch != NULL, FALSE);
161         return klazz->launch(item, context);
162 }
163 
164 /**
165  * brisk_item_get_uri:
166  *
167  * Return the URI for this item, useful for dnd operations
168  *
169  * @note This returns newly allocated memory
170  */
brisk_item_get_uri(BriskItem * item)171 gchar *brisk_item_get_uri(BriskItem *item)
172 {
173         g_assert(item != NULL);
174         BriskItemClass *klazz = BRISK_ITEM_GET_CLASS(item);
175         g_return_val_if_fail(klazz->launch != NULL, NULL);
176         return klazz->get_uri(item);
177 }
178 
179 /*
180  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
181  *
182  * Local variables:
183  * c-basic-offset: 8
184  * tab-width: 8
185  * indent-tabs-mode: nil
186  * End:
187  *
188  * vi: set shiftwidth=8 tabstop=8 expandtab:
189  * :indentSize=8:tabSize=8:noTabs=true:
190  */
191