| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include "config.h" |
| 6 |
|
| 7 |
#include <glib/gi18n.h> |
| 8 |
|
| 9 |
#include <gtk/gtk.h> |
| 10 |
#include <glibtop/mountlist.h> |
| 11 |
#include <glibtop/fsusage.h> |
| 12 |
#include <adwaita.h> |
| 13 |
|
| 14 |
#include "column-view-persister.h" |
| 15 |
#include "disk.h" |
| 16 |
#include "settings-keys.h" |
| 17 |
|
| 18 |
#include "disks.h" |
| 19 |
|
| 20 |
|
| 21 |
struct _GsmDisksView { |
| 22 |
AdwBin parent_instance; |
| 23 |
|
| 24 |
GtkColumnView *column_view; |
| 25 |
GtkSingleSelection *selection; |
| 26 |
GListStore *list_store; |
| 27 |
|
| 28 |
|
| 29 |
GsmColumnViewPersister *persister; |
| 30 |
GSignalGroup *root_signals; |
| 31 |
GSignalGroup *monitor_signals; |
| 32 |
|
| 33 |
int update_interval; |
| 34 |
gboolean show_all_fs; |
| 35 |
|
| 36 |
guint timeout; |
| 37 |
GHashTable *known_directories; |
| 38 |
GSettings *settings; |
| 39 |
GVolumeMonitor *monitor; |
| 40 |
}; |
| 41 |
|
| 42 |
|
| 43 |
G_DEFINE_FINAL_TYPE (GsmDisksView, gsm_disks_view, ADW_TYPE_BIN) |
| 44 |
|
| 45 |
|
| 46 |
enum { |
| 47 |
PROP_0, |
| 48 |
PROP_UPDATE_INTERVAL, |
| 49 |
PROP_SHOW_ALL_FS, |
| 50 |
N_PROPS |
| 51 |
}; |
| 52 |
static GParamSpec *properties[N_PROPS]; |
| 53 |
|
| 54 |
|
| 55 |
static inline void |
| 56 |
clear_timeout (GsmDisksView *self) |
| 57 |
{ |
| 58 |
g_debug ("disks: clear timeout"); |
| 59 |
g_clear_handle_id (&self->timeout, g_source_remove); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
static void |
| 64 |
gsm_disks_view_dispose (GObject *object) |
| 65 |
{ |
| 66 |
GsmDisksView *self = GSM_DISKS_VIEW (object); |
| 67 |
|
| 68 |
clear_timeout (self); |
| 69 |
g_clear_pointer (&self->known_directories, g_hash_table_unref); |
| 70 |
g_clear_object (&self->settings); |
| 71 |
g_clear_object (&self->monitor); |
| 72 |
|
| 73 |
G_OBJECT_CLASS (gsm_disks_view_parent_class)->dispose (object); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
static void |
| 78 |
gsm_disks_view_get_property (GObject *object, |
| 79 |
guint prop_id, |
| 80 |
GValue *value, |
| 81 |
GParamSpec *pspec) |
| 82 |
{ |
| 83 |
GsmDisksView *self = GSM_DISKS_VIEW (object); |
| 84 |
|
| 85 |
switch (prop_id) { |
| 86 |
case PROP_UPDATE_INTERVAL: |
| 87 |
g_value_set_int (value, self->update_interval); |
| 88 |
break; |
| 89 |
case PROP_SHOW_ALL_FS: |
| 90 |
g_value_set_boolean (value, self->show_all_fs); |
| 91 |
break; |
| 92 |
default: |
| 93 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
static void |
| 99 |
fsusage_stats (const glibtop_fsusage *buf, |
| 100 |
uint64_t *bused, |
| 101 |
uint64_t *bfree, |
| 102 |
uint64_t *bavail, |
| 103 |
uint64_t *btotal, |
| 104 |
int *percentage) |
| 105 |
{ |
| 106 |
uint64_t total = buf->blocks * buf->block_size; |
| 107 |
|
| 108 |
if (!total) { |
| 109 |
|
| 110 |
*btotal = *bfree = *bavail = *bused = 0ULL; |
| 111 |
*percentage = 0; |
| 112 |
} else { |
| 113 |
int percent; |
| 114 |
*btotal = total; |
| 115 |
*bfree = buf->bfree * buf->block_size; |
| 116 |
*bavail = buf->bavail * buf->block_size; |
| 117 |
*bused = *btotal - *bfree; |
| 118 |
|
| 119 |
percent = 100 * *bused / (*bused + *bavail); |
| 120 |
*percentage = CLAMP (percent, 0, 100); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
static inline GHashTable * |
| 126 |
get_current_directories (GsmDisksView *self) |
| 127 |
{ |
| 128 |
g_autoptr (GHashTable) current_directories = |
| 129 |
g_hash_table_new_full (g_file_hash, |
| 130 |
(GEqualFunc) g_file_equal, |
| 131 |
g_object_unref, |
| 132 |
NULL); |
| 133 |
GHashTableIter iter; |
| 134 |
GFile *directory; |
| 135 |
|
| 136 |
|
| 137 |
g_hash_table_iter_init (&iter, self->known_directories); |
| 138 |
while (g_hash_table_iter_next (&iter, (gpointer *) &directory, NULL)) { |
| 139 |
g_hash_table_add (current_directories, g_object_ref (directory)); |
| 140 |
} |
| 141 |
|
| 142 |
return g_steal_pointer (¤t_directories); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
static inline void |
| 147 |
drop_leftover_directories (GsmDisksView *self, |
| 148 |
GHashTable *leftover_directories) |
| 149 |
{ |
| 150 |
GHashTableIter iter; |
| 151 |
GFile *directory; |
| 152 |
|
| 153 |
g_hash_table_iter_init (&iter, leftover_directories); |
| 154 |
while (g_hash_table_iter_next (&iter, (gpointer *) &directory, NULL)) { |
| 155 |
g_autoptr (GFile) stolen_key = NULL; |
| 156 |
g_autoptr (GsmDisk) disk = NULL; |
| 157 |
guint position; |
| 158 |
|
| 159 |
if (!g_hash_table_steal_extended (self->known_directories, |
| 160 |
directory, |
| 161 |
(gpointer *) &stolen_key, |
| 162 |
(gpointer *) &disk)) { |
| 163 |
continue; |
| 164 |
} |
| 165 |
|
| 166 |
if (g_list_store_find (self->list_store, disk, &position)) { |
| 167 |
g_list_store_remove (self->list_store, position); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
static inline void |
| 174 |
handle_entry (GsmDisksView *self, |
| 175 |
GPtrArray *new_disks, |
| 176 |
GHashTable *leftover_directories, |
| 177 |
const glibtop_mountentry *entry) |
| 178 |
{ |
| 179 |
glibtop_fsusage usage; |
| 180 |
uint64_t bused, bfree, bavail, btotal; |
| 181 |
int percentage; |
| 182 |
g_autoptr (GFile) device = g_file_new_for_path (entry->devname); |
| 183 |
g_autoptr (GFile) directory = g_file_new_for_path (entry->mountdir); |
| 184 |
GsmDisk *existing_disk = NULL; |
| 185 |
|
| 186 |
glibtop_get_fsusage (&usage, entry->mountdir); |
| 187 |
|
| 188 |
|
| 189 |
g_hash_table_remove (leftover_directories, directory); |
| 190 |
|
| 191 |
if (!self->show_all_fs && usage.blocks == 0) { |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
fsusage_stats (&usage, &bused, &bfree, &bavail, &btotal, &percentage); |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
existing_disk = g_hash_table_lookup (self->known_directories, directory); |
| 202 |
if (existing_disk) { |
| 203 |
gsm_disk_set_device (existing_disk, device); |
| 204 |
g_object_set (existing_disk, |
| 205 |
"type", entry->type, |
| 206 |
"total", btotal, |
| 207 |
"free", bfree, |
| 208 |
"available", bavail, |
| 209 |
"used", bused, |
| 210 |
"percentage", percentage, |
| 211 |
NULL); |
| 212 |
} else { |
| 213 |
g_autoptr (GsmDisk) disk = gsm_disk_new (device, |
| 214 |
directory, |
| 215 |
entry->type, |
| 216 |
btotal, |
| 217 |
bfree, |
| 218 |
bavail, |
| 219 |
bused, |
| 220 |
percentage); |
| 221 |
|
| 222 |
g_hash_table_insert (self->known_directories, |
| 223 |
g_steal_pointer (&directory), |
| 224 |
g_object_ref (disk)); |
| 225 |
g_ptr_array_add (new_disks, g_steal_pointer (&disk)); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
static void |
| 231 |
disks_update (GsmDisksView *self) |
| 232 |
{ |
| 233 |
g_autoptr (GHashTable) leftover_directories = |
| 234 |
get_current_directories (self); |
| 235 |
g_autofree glibtop_mountentry *entries; |
| 236 |
g_autoptr (GPtrArray) new_disks = |
| 237 |
g_ptr_array_new_null_terminated (10, g_object_unref, TRUE); |
| 238 |
glibtop_mountlist mountlist; |
| 239 |
|
| 240 |
entries = glibtop_get_mountlist (&mountlist, self->show_all_fs); |
| 241 |
|
| 242 |
for (guint i = 0; i < mountlist.number; i++) { |
| 243 |
handle_entry (self, new_disks, leftover_directories, &entries[i]); |
| 244 |
} |
| 245 |
|
| 246 |
g_list_store_splice (self->list_store, |
| 247 |
0, |
| 248 |
0, |
| 249 |
new_disks->pdata, |
| 250 |
new_disks->len); |
| 251 |
|
| 252 |
drop_leftover_directories (self, leftover_directories); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
static gboolean |
| 257 |
disks_timeout (gpointer data) |
| 258 |
{ |
| 259 |
disks_update (GSM_DISKS_VIEW (data)); |
| 260 |
|
| 261 |
return G_SOURCE_CONTINUE; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
static inline void |
| 266 |
setup_timeout (GsmDisksView *self, gboolean reset) |
| 267 |
{ |
| 268 |
if (self->timeout && !reset) { |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
clear_timeout (self); |
| 273 |
|
| 274 |
g_debug ("disks: start timeout"); |
| 275 |
g_clear_handle_id (&self->timeout, g_source_remove); |
| 276 |
self->timeout = g_timeout_add (self->update_interval, disks_timeout, self); |
| 277 |
g_source_set_name_by_id (self->timeout, "refresh disks"); |
| 278 |
disks_update (self); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
static void |
| 283 |
gsm_disks_view_set_property (GObject *object, |
| 284 |
guint prop_id, |
| 285 |
const GValue *value, |
| 286 |
GParamSpec *pspec) |
| 287 |
{ |
| 288 |
GsmDisksView *self = GSM_DISKS_VIEW (object); |
| 289 |
|
| 290 |
switch (prop_id) { |
| 291 |
case PROP_UPDATE_INTERVAL: |
| 292 |
{ |
| 293 |
int new_value = g_value_get_int (value); |
| 294 |
|
| 295 |
if (new_value == self->update_interval) { |
| 296 |
return; |
| 297 |
} |
| 298 |
|
| 299 |
self->update_interval = new_value; |
| 300 |
setup_timeout (self, TRUE); |
| 301 |
g_object_notify_by_pspec (object, pspec); |
| 302 |
} |
| 303 |
break; |
| 304 |
case PROP_SHOW_ALL_FS: |
| 305 |
{ |
| 306 |
gboolean new_value = g_value_get_boolean (value); |
| 307 |
|
| 308 |
if (new_value == self->show_all_fs) { |
| 309 |
return; |
| 310 |
} |
| 311 |
|
| 312 |
self->show_all_fs = new_value; |
| 313 |
disks_update (self); |
| 314 |
g_object_notify_by_pspec (object, pspec); |
| 315 |
} |
| 316 |
break; |
| 317 |
default: |
| 318 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
static void |
| 324 |
gsm_disks_view_map (GtkWidget *widget) |
| 325 |
{ |
| 326 |
GTK_WIDGET_CLASS (gsm_disks_view_parent_class)->map (widget); |
| 327 |
|
| 328 |
setup_timeout (GSM_DISKS_VIEW (widget), FALSE); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
static void |
| 333 |
gsm_disks_view_unmap (GtkWidget *widget) |
| 334 |
{ |
| 335 |
GTK_WIDGET_CLASS (gsm_disks_view_parent_class)->unmap (widget); |
| 336 |
|
| 337 |
clear_timeout (GSM_DISKS_VIEW (widget)); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
static void |
| 342 |
mount_changed (G_GNUC_UNUSED GVolumeMonitor *monitor, |
| 343 |
G_GNUC_UNUSED GMount *mount, |
| 344 |
gpointer data) |
| 345 |
{ |
| 346 |
disks_update (GSM_DISKS_VIEW (data)); |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
static char * |
| 351 |
format_size (G_GNUC_UNUSED GObject *object, |
| 352 |
uint64_t size) |
| 353 |
{ |
| 354 |
return g_format_size (size); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
static char * |
| 359 |
format_percentage (G_GNUC_UNUSED GObject *object, |
| 360 |
int percentage) |
| 361 |
{ |
| 362 |
return g_strdup_printf ("%i%%", percentage); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
static void |
| 367 |
did_open (GObject *source, |
| 368 |
GAsyncResult *result, |
| 369 |
gpointer user_data) |
| 370 |
{ |
| 371 |
g_autoptr (GError) error = NULL; |
| 372 |
g_autoptr (GsmDisksView) self = GSM_DISKS_VIEW (user_data); |
| 373 |
|
| 374 |
gsm_disk_open_root_finish (GSM_DISK (source), result, &error); |
| 375 |
|
| 376 |
if (error) { |
| 377 |
AdwDialog *dialog = adw_alert_dialog_new (_("Open Failed"), error->message); |
| 378 |
|
| 379 |
adw_alert_dialog_add_response (ADW_ALERT_DIALOG (dialog), |
| 380 |
"close", |
| 381 |
_("_Close")); |
| 382 |
|
| 383 |
adw_dialog_present (dialog, GTK_WIDGET (self)); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
static void |
| 389 |
activate_disk (GtkColumnView *view, guint position, gpointer user_data) |
| 390 |
{ |
| 391 |
GsmDisksView *self = GSM_DISKS_VIEW (user_data); |
| 392 |
GListModel *selection = G_LIST_MODEL (self->selection); |
| 393 |
g_autoptr (GsmDisk) disk = |
| 394 |
GSM_DISK (g_list_model_get_object (selection, position)); |
| 395 |
|
| 396 |
gsm_disk_open_root (disk, |
| 397 |
GTK_WINDOW (gtk_widget_get_root (GTK_WIDGET (view))), |
| 398 |
NULL, |
| 399 |
did_open, |
| 400 |
g_object_ref (self)); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
static void |
| 405 |
gsm_disks_view_class_init (GsmDisksViewClass *klass) |
| 406 |
{ |
| 407 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 408 |
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); |
| 409 |
|
| 410 |
object_class->dispose = gsm_disks_view_dispose; |
| 411 |
object_class->get_property = gsm_disks_view_get_property; |
| 412 |
object_class->set_property = gsm_disks_view_set_property; |
| 413 |
|
| 414 |
widget_class->map = gsm_disks_view_map; |
| 415 |
widget_class->unmap = gsm_disks_view_unmap; |
| 416 |
|
| 417 |
properties[PROP_UPDATE_INTERVAL] = |
| 418 |
g_param_spec_int ("update-interval", NULL, NULL, |
| 419 |
0, G_MAXINT32, 0, |
| 420 |
G_PARAM_READWRITE); |
| 421 |
properties[PROP_SHOW_ALL_FS] = |
| 422 |
g_param_spec_boolean ("show-all-fs", NULL, NULL, |
| 423 |
FALSE, |
| 424 |
G_PARAM_READWRITE); |
| 425 |
|
| 426 |
g_object_class_install_properties (object_class, N_PROPS, properties); |
| 427 |
|
| 428 |
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/gnome-system-monitor/data/disks.ui"); |
| 429 |
|
| 430 |
gtk_widget_class_bind_template_child (widget_class, GsmDisksView, column_view); |
| 431 |
gtk_widget_class_bind_template_child (widget_class, GsmDisksView, selection); |
| 432 |
gtk_widget_class_bind_template_child (widget_class, GsmDisksView, list_store); |
| 433 |
gtk_widget_class_bind_template_child (widget_class, GsmDisksView, persister); |
| 434 |
gtk_widget_class_bind_template_child (widget_class, GsmDisksView, root_signals); |
| 435 |
gtk_widget_class_bind_template_child (widget_class, GsmDisksView, monitor_signals); |
| 436 |
|
| 437 |
gtk_widget_class_bind_template_callback (widget_class, format_size); |
| 438 |
gtk_widget_class_bind_template_callback (widget_class, format_percentage); |
| 439 |
gtk_widget_class_bind_template_callback (widget_class, activate_disk); |
| 440 |
|
| 441 |
g_type_ensure (GSM_TYPE_COLUMN_VIEW_PERSISTER); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
static void |
| 446 |
suspended_notified (GtkWindow *window, |
| 447 |
G_GNUC_UNUSED GParamSpec *pspec, |
| 448 |
gpointer user_data) |
| 449 |
{ |
| 450 |
GsmDisksView *self = GSM_DISKS_VIEW (user_data); |
| 451 |
|
| 452 |
if (gtk_window_is_suspended (window)) { |
| 453 |
clear_timeout (self); |
| 454 |
} else if (gtk_widget_get_mapped (GTK_WIDGET (window))) { |
| 455 |
setup_timeout (self, FALSE); |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
static void |
| 461 |
gsm_disks_view_init (GsmDisksView *self) |
| 462 |
{ |
| 463 |
gtk_widget_init_template (GTK_WIDGET (self)); |
| 464 |
|
| 465 |
self->known_directories = g_hash_table_new_full (g_file_hash, |
| 466 |
(GEqualFunc) g_file_equal, |
| 467 |
g_object_unref, |
| 468 |
g_object_unref); |
| 469 |
|
| 470 |
self->settings = g_settings_new (GSM_GSETTINGS_SCHEMA); |
| 471 |
|
| 472 |
self->monitor = g_volume_monitor_get (); |
| 473 |
|
| 474 |
g_signal_group_connect (self->monitor_signals, |
| 475 |
"mount-added", G_CALLBACK (mount_changed), |
| 476 |
self); |
| 477 |
g_signal_group_connect (self->monitor_signals, |
| 478 |
"mount-changed", G_CALLBACK (mount_changed), |
| 479 |
self); |
| 480 |
g_signal_group_connect (self->monitor_signals, |
| 481 |
"mount-removed", G_CALLBACK (mount_changed), |
| 482 |
self); |
| 483 |
g_signal_group_set_target (self->monitor_signals, self->monitor); |
| 484 |
|
| 485 |
g_settings_bind (self->settings, GSM_SETTING_DISKS_UPDATE_INTERVAL, |
| 486 |
G_OBJECT (self), "update-interval", G_SETTINGS_BIND_DEFAULT); |
| 487 |
|
| 488 |
g_settings_bind (self->settings, GSM_SETTING_SHOW_ALL_FS, |
| 489 |
G_OBJECT (self), GSM_SETTING_SHOW_ALL_FS, G_SETTINGS_BIND_DEFAULT); |
| 490 |
|
| 491 |
g_signal_group_connect (self->root_signals, |
| 492 |
"notify::suspended", |
| 493 |
G_CALLBACK (suspended_notified), |
| 494 |
self); |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
GListModel * |
| 499 |
gsm_disks_view_get_columns (GsmDisksView *self) |
| 500 |
{ |
| 501 |
g_return_val_if_fail (GSM_IS_DISKS_VIEW (self), NULL); |
| 502 |
|
| 503 |
return gtk_column_view_get_columns (self->column_view); |
| 504 |
} |