summaryrefslogtreecommitdiffstats
path: root/src/disks.cpp
blob: fe79fae25ba6821c57bce6590db11470cf8d4fe9 (plain)
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#include <config.h>

#include <gtk/gtk.h>
#include <glibtop/mountlist.h>
#include <glibtop/fsusage.h>
#include <glib/gi18n.h>

#include "disks.h"
#include "application.h"
#include "util.h"
#include "settings-keys.h"
#include "legacy/treeview.h"

enum DiskColumns
{
    /* string columns* */
    DISK_DEVICE,
    DISK_DIR,
    DISK_TYPE,
    DISK_TOTAL,
    DISK_FREE,
    DISK_AVAIL,
    /* USED has to be the last column */
    DISK_USED,
    // then invisible columns
    /* PixBuf column */
    DISK_ICON,
    /* numeric columns */
    DISK_USED_PERCENTAGE,
    DISK_N_COLUMNS
};

static void
cb_sort_changed (GtkTreeSortable *model, gpointer data)
{
    GsmApplication *app = (GsmApplication *) data;

    gsm_tree_view_save_state (GSM_TREE_VIEW (app->disk_list));
}

static void
fsusage_stats(const glibtop_fsusage *buf,
              guint64 *bused, guint64 *bfree, guint64 *bavail, guint64 *btotal,
              gint *percentage)
{
    guint64 total = buf->blocks * buf->block_size;

    if (!total) {
        /* not a real device */
        *btotal = *bfree = *bavail = *bused = 0ULL;
        *percentage = 0;
    } else {
        int percent;
        *btotal = total;
        *bfree = buf->bfree * buf->block_size;
        *bavail = buf->bavail * buf->block_size;
        *bused = *btotal - *bfree;
        /* percent = 100.0f * *bused / *btotal; */
        percent = 100 * *bused / (*bused + *bavail);
        *percentage = CLAMP(percent, 0, 100);
    }
}

static const char* get_icon_for_path(const char* path)
{
    GVolumeMonitor *monitor;
    GList *mounts;
    uint i;
    GMount *mount;
    GIcon *icon;
    const char* name = "";

    monitor = g_volume_monitor_get ();
    mounts = g_volume_monitor_get_mounts (monitor);

    for (i = 0; i < g_list_length (mounts); i++) {
        mount = G_MOUNT (g_list_nth_data(mounts, i));
        if (strcmp(g_mount_get_name(mount), path))
            continue;

        icon = g_mount_get_icon (mount);

        if (!icon)
            continue;
        name = g_icon_to_string (icon);
        g_object_unref (icon);
    }

    g_list_free_full (mounts, g_object_unref);
    return name;

}

static GdkPixbuf*
get_icon_for_device(const char *mountpoint)
{
    const char* icon_name = get_icon_for_path(mountpoint);
    if (!strcmp(icon_name, ""))
        // FIXME: defaults to a safe value
        icon_name = "drive-harddisk"; // get_icon_for_path("/");
    return gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), icon_name, 24, GTK_ICON_LOOKUP_USE_BUILTIN, NULL);
}


static gboolean
find_disk_in_model(GtkTreeModel *model, const char *mountpoint,
                   GtkTreeIter *result)
{
    GtkTreeIter iter;
    gboolean found = FALSE;

    if (gtk_tree_model_get_iter_first(model, &iter)) {
        do {
            char *dir;

            gtk_tree_model_get(model, &iter,
                               DISK_DIR, &dir,
                               -1);

            if (dir && !strcmp(dir, mountpoint)) {
                *result = iter;
                found = TRUE;
            }

            g_free(dir);

        } while (!found && gtk_tree_model_iter_next(model, &iter));
    }

    return found;
}



static void
remove_old_disks(GtkTreeModel *model, const glibtop_mountentry *entries, guint n)
{
    GtkTreeIter iter;

    if (!gtk_tree_model_get_iter_first(model, &iter))
        return;

    while (true) {
        char *dir;
        guint i;
        gboolean found = FALSE;

        gtk_tree_model_get(model, &iter,
                           DISK_DIR, &dir,
                           -1);

        for (i = 0; i != n; ++i) {
            if (!strcmp(dir, entries[i].mountdir)) {
                found = TRUE;
                break;
            }
        }

        g_free(dir);

        if (!found) {
            if (!gtk_list_store_remove(GTK_LIST_STORE(model), &iter))
                break;
            else
                continue;
        }

        if (!gtk_tree_model_iter_next(model, &iter))
            break;
    }
}



static void
add_disk(GtkListStore *list, const glibtop_mountentry *entry, bool show_all_fs)
{
    GdkPixbuf* pixbuf;
    GtkTreeIter iter;
    glibtop_fsusage usage;
    guint64 bused, bfree, bavail, btotal;
    gint percentage;

    glibtop_get_fsusage(&usage, entry->mountdir);

    if (not show_all_fs and usage.blocks == 0) {
        if (find_disk_in_model(GTK_TREE_MODEL(list), entry->mountdir, &iter))
            gtk_list_store_remove(list, &iter);
        return;
    }

    fsusage_stats(&usage, &bused, &bfree, &bavail, &btotal, &percentage);
    pixbuf = get_icon_for_device(entry->mountdir);

    /* if we can find a row with the same mountpoint, we get it but we
       still need to update all the fields.
       This makes selection persistent.
    */
    if (!find_disk_in_model(GTK_TREE_MODEL(list), entry->mountdir, &iter))
        gtk_list_store_append(list, &iter);

    gtk_list_store_set(list, &iter,
                       DISK_ICON, pixbuf,
                       DISK_DEVICE, entry->devname,
                       DISK_DIR, entry->mountdir,
                       DISK_TYPE, entry->type,
                       DISK_USED_PERCENTAGE, percentage,
                       DISK_TOTAL, btotal,
                       DISK_FREE, bfree,
                       DISK_AVAIL, bavail,
                       DISK_USED, bused,
                       -1);
}

static void
mount_changed (GVolumeMonitor *monitor, GMount *mount, GsmApplication *app)
{
    disks_update(app);
}

static gboolean
cb_timeout (gpointer data)
{
    GsmApplication *app = (GsmApplication *) data;
    disks_update (app);

    return G_SOURCE_CONTINUE;
}

void
disks_update(GsmApplication *app)
{
    GtkListStore *list;
    glibtop_mountentry * entries;
    glibtop_mountlist mountlist;
    guint i;
    gboolean show_all_fs;

    list = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(app->disk_list)));
    show_all_fs = app->settings->get_boolean (GSM_SETTING_SHOW_ALL_FS);
    entries = glibtop_get_mountlist (&mountlist, show_all_fs);

    remove_old_disks(GTK_TREE_MODEL(list), entries, mountlist.number);

    for (i = 0; i < mountlist.number; i++)
        add_disk(list, &entries[i], show_all_fs);

    g_free(entries);
}

static void
init_volume_monitor (GsmApplication *app)
{
    GVolumeMonitor *monitor = g_volume_monitor_get ();

    g_signal_connect (monitor, "mount-added", G_CALLBACK (mount_changed), app);
    g_signal_connect (monitor, "mount-changed", G_CALLBACK (mount_changed), app);
    g_signal_connect (monitor, "mount-removed", G_CALLBACK (mount_changed), app);
}

void
disks_freeze (GsmApplication *app)
{
  if (app->disk_timeout) {
      g_source_remove (app->disk_timeout);
      app->disk_timeout = 0;
  }
}

void
disks_thaw (GsmApplication *app)
{
  if (app->disk_timeout)
      return;

  app->disk_timeout = g_timeout_add (app->config.disks_update_interval,
                                     cb_timeout,
                                     app);
}

void
disks_reset_timeout (GsmApplication *app)
{
    disks_freeze (app);
    disks_thaw (app);
}

static void
cb_disk_columns_changed(GtkTreeView *treeview, gpointer data)
{
    gsm_tree_view_save_state (GSM_TREE_VIEW (treeview));
}


static void open_dir(GtkTreeView       *tree_view,
                     GtkTreePath       *path,
                     GtkTreeViewColumn *column,
                     gpointer          user_data)
{
    GtkTreeIter iter;
    GtkTreeModel *model;
    char *dir, *url;

    model = gtk_tree_view_get_model(tree_view);

    if (!gtk_tree_model_get_iter(model, &iter, path)) {
        char *p;
        p = gtk_tree_path_to_string(path);
        g_warning("Cannot get iter for path '%s'\n", p);
        g_free(p);
        return;
    }

    gtk_tree_model_get(model, &iter, DISK_DIR, &dir, -1);

    url = g_strdup_printf("file://%s", dir);

    GError* error = 0;
    if (!g_app_info_launch_default_for_uri(url, NULL, &error)) {
        g_warning("Cannot open '%s' : %s\n", url, error->message);
        g_error_free(error);
    }

    g_free(url);
    g_free(dir);
}

static void
cb_disk_list_destroying (GtkWidget *self, gpointer data)
{
    g_signal_handlers_disconnect_by_func(self, (gpointer) cb_disk_columns_changed, data);
    
    g_signal_handlers_disconnect_by_func (gtk_tree_view_get_model (GTK_TREE_VIEW(self)),
                                          (gpointer) cb_sort_changed,
                                          data);
}


void
create_disk_view(GsmApplication *app, GtkBuilder *builder)
{
    GtkScrolledWindow *scrolled;
    GsmTreeView *disk_tree;
    GtkListStore *model;
    GtkTreeViewColumn *col;
    GtkCellRenderer *cell;
    PangoAttrList *attrs = NULL;
    guint i;

    init_volume_monitor (app);
    const gchar * const titles[] = {
        N_("Device"),
        N_("Directory"),
        N_("Type"),
        N_("Total"),
        N_("Free"),
        N_("Available"),
        N_("Used")
    };

    scrolled = GTK_SCROLLED_WINDOW (gtk_builder_get_object (builder, "disks_scrolled"));

    model = gtk_list_store_new(DISK_N_COLUMNS,      /* n columns */
                               G_TYPE_STRING,       /* DISK_DEVICE */
                               G_TYPE_STRING,       /* DISK_DIR */
                               G_TYPE_STRING,       /* DISK_TYPE */
                               G_TYPE_UINT64,       /* DISK_TOTAL */
                               G_TYPE_UINT64,       /* DISK_FREE */
                               G_TYPE_UINT64,       /* DISK_AVAIL */
                               G_TYPE_UINT64,       /* DISK_USED */
                               GDK_TYPE_PIXBUF,     /* DISK_ICON */
                               G_TYPE_INT           /* DISK_USED_PERCENTAGE */
        );
    disk_tree = gsm_tree_view_new (g_settings_get_child (app->settings->gobj(), GSM_SETTINGS_CHILD_DISKS), TRUE);
    gtk_tree_view_set_model (GTK_TREE_VIEW (disk_tree), GTK_TREE_MODEL (model));

    g_signal_connect(G_OBJECT(disk_tree), "row-activated", G_CALLBACK(open_dir), NULL);
    app->disk_list = disk_tree;
    gtk_container_add(GTK_CONTAINER(scrolled), GTK_WIDGET (disk_tree));
    g_object_unref(G_OBJECT(model));

    /* icon + device */

    col = gtk_tree_view_column_new();
    cell = gtk_cell_renderer_pixbuf_new();
    
    gtk_tree_view_column_pack_start(col, cell, FALSE);
    gtk_tree_view_column_set_min_width(col, 30);
    gtk_tree_view_column_set_attributes(col, cell, "pixbuf", DISK_ICON,
                                        NULL);

    cell = gtk_cell_renderer_text_new();
    gtk_tree_view_column_pack_start(col, cell, FALSE);
    gtk_tree_view_column_set_attributes(col, cell, "text", DISK_DEVICE,
                                        NULL);
    gtk_tree_view_column_set_title(col, _(titles[DISK_DEVICE]));
    gtk_tree_view_column_set_sort_column_id(col, DISK_DEVICE);
    gtk_tree_view_column_set_reorderable(col, TRUE);
    gtk_tree_view_column_set_resizable(col, TRUE);
    gtk_tree_view_column_set_sizing(col, GTK_TREE_VIEW_COLUMN_FIXED);
    gsm_tree_view_append_and_bind_column (GSM_TREE_VIEW (disk_tree), col);


    /* sizes - used */

    for (i = DISK_DIR; i <= DISK_AVAIL; i++) {
        cell = gtk_cell_renderer_text_new();
        col = gtk_tree_view_column_new();
        gtk_tree_view_column_pack_start(col, cell, TRUE);
        gtk_tree_view_column_set_title(col, _(titles[i]));
        gtk_tree_view_column_set_resizable(col, TRUE);
        gtk_tree_view_column_set_sort_column_id(col, i);
        gtk_tree_view_column_set_reorderable(col, TRUE);
        gtk_tree_view_column_set_min_width(col, i == DISK_TYPE ? 40 : 72);
        gtk_tree_view_column_set_sizing(col, GTK_TREE_VIEW_COLUMN_FIXED);
        gsm_tree_view_append_and_bind_column (GSM_TREE_VIEW (disk_tree), col);
        switch (i) {
            case DISK_TOTAL:
            case DISK_FREE:
            case DISK_AVAIL:
                gtk_tree_view_column_set_cell_data_func(col, cell,
                                                        &procman::size_si_cell_data_func,
                                                        GUINT_TO_POINTER(i),
                                                        NULL);

                attrs = make_tnum_attr_list ();
                g_object_set (cell,
                              "attributes", attrs,
                              "xalign", 1.0f,
                              NULL);
                g_clear_pointer (&attrs, pango_attr_list_unref);

                break;

            default:
                gtk_tree_view_column_set_attributes(col, cell,
                                                    "text", i,
                                                    NULL);
                break;
        }
    }

    /* used + percentage */

    col = gtk_tree_view_column_new();
    cell = gtk_cell_renderer_text_new();

    attrs = make_tnum_attr_list ();
    g_object_set (cell,
                  "attributes", attrs,
                  "xalign", 1.0f,
                  NULL);
    g_clear_pointer (&attrs, pango_attr_list_unref);

    gtk_tree_view_column_set_min_width(col, 72);
    gtk_tree_view_column_set_sizing(col, GTK_TREE_VIEW_COLUMN_FIXED);
    gtk_tree_view_column_pack_start(col, cell, FALSE);
    gtk_tree_view_column_set_cell_data_func(col, cell,
                                            &procman::size_si_cell_data_func,
                                            GUINT_TO_POINTER(DISK_USED),
                                            NULL);
    gtk_tree_view_column_set_title(col, _(titles[DISK_USED]));

    cell = gtk_cell_renderer_progress_new();
    gtk_cell_renderer_set_padding(cell, 4.0f, 4.0f);
    gtk_tree_view_column_pack_start(col, cell, TRUE);
    gtk_tree_view_column_set_attributes(col, cell, "value",
                                        DISK_USED_PERCENTAGE, NULL);
    gtk_tree_view_column_set_resizable(col, TRUE);
    gtk_tree_view_column_set_sort_column_id(col, DISK_USED);
    gtk_tree_view_column_set_reorderable(col, TRUE);
    gsm_tree_view_append_and_bind_column (GSM_TREE_VIEW (disk_tree), col);

    /* numeric sort */

    gsm_tree_view_load_state (GSM_TREE_VIEW (disk_tree));
    g_signal_connect (G_OBJECT(disk_tree), "destroy",
                      G_CALLBACK(cb_disk_list_destroying),
                      app);

    g_signal_connect (G_OBJECT(disk_tree), "columns-changed",
                      G_CALLBACK(cb_disk_columns_changed), app);
                      
    g_signal_connect (G_OBJECT (model), "sort-column-changed",
                      G_CALLBACK (cb_sort_changed), app);

    app->settings->signal_changed(GSM_SETTING_SHOW_ALL_FS).connect ([app](const Glib::ustring&) { disks_update (app); disks_reset_timeout (app); });

    gtk_widget_show (GTK_WIDGET (disk_tree));
}