| 1 |
/* font-manager-source.c |
| 2 |
* |
| 3 |
* Copyright (C) 2009-2025 Jerry Casiano |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. |
| 17 |
* |
| 18 |
* If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "font-manager-source.h" |
| 22 |
|
| 23 |
/** |
| 24 |
* SECTION: font-manager-source |
| 25 |
* @short_description: Font directory entry |
| 26 |
* @title: Font Source |
| 27 |
* @include: font-manager-source.h |
| 28 |
* |
| 29 |
* #FontManagerSource represents a user font directory. |
| 30 |
*/ |
| 31 |
|
| 32 |
struct _FontManagerSource |
| 33 |
{ |
| 34 |
GObject parent; |
| 35 |
}; |
| 36 |
|
| 37 |
typedef struct |
| 38 |
{ |
| 39 |
gchar *name; |
| 40 |
gchar *path; |
| 41 |
gboolean active; |
| 42 |
GFile *file; |
| 43 |
GFileMonitor *monitor; |
| 44 |
} |
| 45 |
FontManagerSourcePrivate; |
| 46 |
|
| 47 |
G_DEFINE_TYPE_WITH_PRIVATE(FontManagerSource, font_manager_source, G_TYPE_OBJECT) |
| 48 |
|
| 49 |
enum |
| 50 |
{ |
| 51 |
PROP_RESERVED, |
| 52 |
PROP_NAME, |
| 53 |
PROP_ICON_NAME, |
| 54 |
PROP_PATH, |
| 55 |
PROP_ACTIVE, |
| 56 |
PROP_AVAILABLE, |
| 57 |
PROP_FILE, |
| 58 |
N_PROPERTIES |
| 59 |
}; |
| 60 |
|
| 61 |
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, }; |
| 62 |
|
| 63 |
enum |
| 64 |
{ |
| 65 |
CHANGED, |
| 66 |
N_SIGNALS |
| 67 |
}; |
| 68 |
|
| 69 |
static guint signals[N_SIGNALS]; |
| 70 |
|
| 71 |
#define DEFAULT_PARAM_FLAGS (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS) |
| 72 |
|
| 73 |
static const gchar *_available = "folder-symbolic"; |
| 74 |
static const gchar *_unavailable = "action-unavailable-symbolic"; |
| 75 |
|
| 76 |
static void |
| 77 |
font_manager_source_constructed (GObject *gobject) |
| 78 |
{ |
| 79 |
g_return_if_fail(gobject != NULL); |
| 80 |
font_manager_source_update(FONT_MANAGER_SOURCE(gobject)); |
| 81 |
G_OBJECT_CLASS(font_manager_source_parent_class)->constructed(gobject); |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
static void |
| 86 |
font_manager_source_dispose (GObject *gobject) |
| 87 |
{ |
| 88 |
g_return_if_fail(gobject != NULL); |
| 89 |
FontManagerSource *self = FONT_MANAGER_SOURCE(gobject); |
| 90 |
FontManagerSourcePrivate *priv = font_manager_source_get_instance_private(self); |
| 91 |
g_clear_pointer(&priv->name, g_free); |
| 92 |
g_clear_pointer(&priv->path, g_free); |
| 93 |
g_clear_object(&priv->file); |
| 94 |
g_clear_object(&priv->monitor); |
| 95 |
G_OBJECT_CLASS(font_manager_source_parent_class)->dispose(gobject); |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
static void |
| 100 |
font_manager_source_get_property (GObject *gobject, |
| 101 |
guint property_id, |
| 102 |
GValue *value, |
| 103 |
GParamSpec *pspec) |
| 104 |
{ |
| 105 |
g_return_if_fail(gobject != NULL); |
| 106 |
FontManagerSource *self = FONT_MANAGER_SOURCE(gobject); |
| 107 |
FontManagerSourcePrivate *priv = font_manager_source_get_instance_private(self); |
| 108 |
gboolean available = priv->file != NULL ? g_file_query_exists(priv->file, NULL) : FALSE; |
| 109 |
switch (property_id) { |
| 110 |
case PROP_NAME: |
| 111 |
g_value_set_string(value, priv->name); |
| 112 |
break; |
| 113 |
case PROP_ICON_NAME: |
| 114 |
g_value_set_string(value, available ? _available : _unavailable); |
| 115 |
break; |
| 116 |
case PROP_PATH: |
| 117 |
g_value_set_string(value, priv->path); |
| 118 |
break; |
| 119 |
case PROP_ACTIVE: |
| 120 |
g_value_set_boolean(value, priv->active); |
| 121 |
break; |
| 122 |
case PROP_AVAILABLE: |
| 123 |
g_value_set_boolean(value, available); |
| 124 |
break; |
| 125 |
case PROP_FILE: |
| 126 |
g_value_set_object(value, priv->file); |
| 127 |
break; |
| 128 |
default: |
| 129 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); |
| 130 |
break; |
| 131 |
} |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
static void |
| 136 |
font_manager_source_set_property (GObject *gobject, |
| 137 |
guint property_id, |
| 138 |
const GValue *value, |
| 139 |
GParamSpec *pspec) |
| 140 |
{ |
| 141 |
g_return_if_fail(gobject != NULL); |
| 142 |
FontManagerSource *self = FONT_MANAGER_SOURCE(gobject); |
| 143 |
FontManagerSourcePrivate *priv = font_manager_source_get_instance_private(self); |
| 144 |
GFile *new_file = NULL; |
| 145 |
switch (property_id) { |
| 146 |
case PROP_ACTIVE: |
| 147 |
priv->active = g_value_get_boolean(value); |
| 148 |
break; |
| 149 |
case PROP_FILE: |
| 150 |
new_file = g_value_get_object(value); |
| 151 |
if (new_file == priv->file) |
| 152 |
return; |
| 153 |
if (priv->file) |
| 154 |
g_clear_object(&priv->file); |
| 155 |
priv->file = new_file ? g_object_ref(new_file) : NULL; |
| 156 |
font_manager_source_update(self); |
| 157 |
break; |
| 158 |
default: |
| 159 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); |
| 160 |
break; |
| 161 |
} |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
static void |
| 166 |
font_manager_source_class_init (FontManagerSourceClass *klass) |
| 167 |
{ |
| 168 |
GObjectClass *object_class = G_OBJECT_CLASS(klass); |
| 169 |
object_class->constructed = font_manager_source_constructed; |
| 170 |
object_class->dispose = font_manager_source_dispose; |
| 171 |
object_class->get_property = font_manager_source_get_property; |
| 172 |
object_class->set_property = font_manager_source_set_property; |
| 173 |
|
| 174 |
/** |
| 175 |
* FontManagerSource::changed: |
| 176 |
* @self: #FontManagerSource which changed |
| 177 |
* @file: #GFile |
| 178 |
* @other_file: #GFile |
| 179 |
* @event_type: #GFileMonitorEvent |
| 180 |
* |
| 181 |
* Emitted when source has changed. |
| 182 |
* |
| 183 |
* See #GFileMonitor's #GFileMonitor::changed signal for parameter details. |
| 184 |
*/ |
| 185 |
signals[CHANGED] = g_signal_new(g_intern_static_string("changed"), |
| 186 |
G_TYPE_FROM_CLASS(object_class), |
| 187 |
G_SIGNAL_RUN_LAST, |
| 188 |
0, |
| 189 |
NULL, NULL, NULL, |
| 190 |
G_TYPE_NONE, |
| 191 |
3, |
| 192 |
G_TYPE_FILE, |
| 193 |
G_TYPE_FILE, |
| 194 |
G_TYPE_FILE_MONITOR_EVENT); |
| 195 |
|
| 196 |
/** |
| 197 |
* FontManagerSource:name: |
| 198 |
* |
| 199 |
* A string suitable for display in a user interface. |
| 200 |
*/ |
| 201 |
obj_properties[PROP_NAME] = g_param_spec_string("name", |
| 202 |
NULL, |
| 203 |
"Name", |
| 204 |
NULL, |
| 205 |
DEFAULT_PARAM_FLAGS); |
| 206 |
|
| 207 |
/** |
| 208 |
* FontManagerSource:icon-name: |
| 209 |
* |
| 210 |
* Name of icon which should be used to represent this source. |
| 211 |
*/ |
| 212 |
obj_properties[PROP_ICON_NAME] = g_param_spec_string("icon-name", |
| 213 |
NULL, |
| 214 |
"Icon name", |
| 215 |
NULL, |
| 216 |
DEFAULT_PARAM_FLAGS); |
| 217 |
|
| 218 |
/** |
| 219 |
* FontManagerSource:path: |
| 220 |
* |
| 221 |
* Full path to source. |
| 222 |
*/ |
| 223 |
obj_properties[PROP_PATH] = g_param_spec_string("path", |
| 224 |
NULL, |
| 225 |
"Filepath", |
| 226 |
NULL, |
| 227 |
DEFAULT_PARAM_FLAGS); |
| 228 |
|
| 229 |
/** |
| 230 |
* FontManagerSource:active: |
| 231 |
* |
| 232 |
* Whether source is currently active or not. |
| 233 |
*/ |
| 234 |
obj_properties[PROP_ACTIVE] = g_param_spec_boolean("active", |
| 235 |
NULL, |
| 236 |
"Whether source is enabled or not", |
| 237 |
FALSE, |
| 238 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); |
| 239 |
|
| 240 |
/** |
| 241 |
* FontManagerSource:available: |
| 242 |
* |
| 243 |
* Whether source is currently available or not. |
| 244 |
*/ |
| 245 |
obj_properties[PROP_AVAILABLE] = g_param_spec_boolean("available", |
| 246 |
NULL, |
| 247 |
"Whether source is available or not", |
| 248 |
FALSE, |
| 249 |
DEFAULT_PARAM_FLAGS); |
| 250 |
|
| 251 |
/** |
| 252 |
* FontManagerSource:file: |
| 253 |
* |
| 254 |
* The #GFile backing this source |
| 255 |
*/ |
| 256 |
obj_properties[PROP_FILE] = g_param_spec_object("file", |
| 257 |
NULL, |
| 258 |
"#GFile backing this source", |
| 259 |
G_TYPE_FILE, |
| 260 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); |
| 261 |
|
| 262 |
g_object_class_install_properties(object_class, N_PROPERTIES, obj_properties); |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
static void |
| 267 |
font_manager_source_init (G_GNUC_UNUSED FontManagerSource *self) |
| 268 |
{ |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
static void |
| 273 |
font_manager_source_emit_changed (G_GNUC_UNUSED GFileMonitor *monitor, |
| 274 |
GFile *file, |
| 275 |
GFile *other_file, |
| 276 |
GFileMonitorEvent event_type, |
| 277 |
gpointer user_data) |
| 278 |
{ |
| 279 |
g_return_if_fail(user_data != NULL); |
| 280 |
FontManagerSource *self = FONT_MANAGER_SOURCE(user_data); |
| 281 |
FontManagerSourcePrivate *priv = font_manager_source_get_instance_private(self); |
| 282 |
if (other_file != NULL) { |
| 283 |
g_clear_object(&priv->file); |
| 284 |
priv->file = g_object_ref(G_FILE_MONITOR_EVENT_MOVED_IN ? file : other_file); |
| 285 |
} |
| 286 |
font_manager_source_update(self); |
| 287 |
g_signal_emit(self, signals[CHANGED], 0, file, other_file, event_type); |
| 288 |
return; |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* font_manager_source_get_status_message: |
| 293 |
* @self: #FontManagerSource |
| 294 |
* |
| 295 |
* Returns: (transfer full) (nullable): A newly allocated string suitable |
| 296 |
* for display in a user interface or %NULL. Free the result using #g_free(). |
| 297 |
*/ |
| 298 |
gchar * |
| 299 |
font_manager_source_get_status_message (FontManagerSource *self) |
| 300 |
{ |
| 301 |
g_return_val_if_fail(self != NULL, FALSE); |
| 302 |
FontManagerSourcePrivate *priv = font_manager_source_get_instance_private(self); |
| 303 |
if (priv->path && !g_file_query_exists(priv->file, NULL)) |
| 304 |
return g_strdup(priv->path); |
| 305 |
return priv->path != NULL ? g_path_get_dirname(priv->path) : g_strdup(_("Source Unavailable")); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* font_manager_source_update: |
| 310 |
* @self: #FontManagerSource |
| 311 |
* |
| 312 |
* Update the status of source. |
| 313 |
*/ |
| 314 |
void |
| 315 |
font_manager_source_update (FontManagerSource *self) |
| 316 |
{ |
| 317 |
g_return_if_fail(self != NULL); |
| 318 |
FontManagerSourcePrivate *priv = font_manager_source_get_instance_private(self); |
| 319 |
g_free(priv->name); |
| 320 |
g_free(priv->path); |
| 321 |
priv->name = g_strdup(_("Source Unavailable")); |
| 322 |
priv->path = priv->file ? g_file_get_path(priv->file) : g_strdup(_("Source Unavailable")); |
| 323 |
priv->active = FALSE; |
| 324 |
if (priv->file == NULL || !g_file_query_exists(priv->file, NULL)) |
| 325 |
return; |
| 326 |
g_autoptr(GFileInfo) fileinfo = g_file_query_info(priv->file, |
| 327 |
G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, |
| 328 |
G_FILE_QUERY_INFO_NONE, |
| 329 |
NULL, NULL); |
| 330 |
if (fileinfo != NULL) { |
| 331 |
g_free(priv->name); |
| 332 |
priv->name = g_markup_escape_text(g_file_info_get_display_name(fileinfo), -1); |
| 333 |
} |
| 334 |
if (priv->monitor) |
| 335 |
g_clear_object(&priv->monitor); |
| 336 |
priv->monitor = g_file_monitor_directory(priv->file, G_FILE_MONITOR_WATCH_MOUNTS | G_FILE_MONITOR_WATCH_MOVES, NULL, NULL); |
| 337 |
if (priv->monitor != NULL) |
| 338 |
g_signal_connect(priv->monitor, "changed", G_CALLBACK(font_manager_source_emit_changed), self); |
| 339 |
else |
| 340 |
g_warning(G_STRLOC ": Failed to create file monitor for %s", priv->path); |
| 341 |
return; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* font_manager_source_new: |
| 346 |
* @file: (nullable): A #GFile to create a new #FontManagerSource for. |
| 347 |
* |
| 348 |
* Returns: (transfer full): A newly created #FontManagerSource. |
| 349 |
* Free the returned object using #g_object_unref(). |
| 350 |
*/ |
| 351 |
FontManagerSource * |
| 352 |
font_manager_source_new (GFile *file) |
| 353 |
{ |
| 354 |
return g_object_new(FONT_MANAGER_TYPE_SOURCE, "file", file, NULL);; |
| 355 |
} |
| 356 |
|