File: notifier.c

package info (click to toggle)
elektroid 3.2.3-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,956 kB
  • sloc: ansic: 25,469; sh: 461; makefile: 292; xml: 72
file content (154 lines) | stat: -rw-r--r-- 4,057 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
/*
 *   notifier.c
 *   Copyright (C) 2021 David García Goñi <dagargo@gmail.com>
 *
 *   This file is part of Elektroid.
 *
 *   Elektroid 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 3 of the License, or
 *   (at your option) any later version.
 *
 *   Elektroid 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 Elektroid. If not, see <http://www.gnu.org/licenses/>.
 */

#include "notifier.h"

#define NOTIFIER_RATE_LIMIT 1000

static gboolean
notifier_go_up (gpointer data)
{
  struct browser *browser = data;
  browser_go_up (NULL, browser);
  return FALSE;
}

static void
notifier_parent_changed (GFileMonitor *self, GFile *file, GFile *other_file,
			 GFileMonitorEvent event_type, gpointer user_data)
{
  struct notifier *notifier = user_data;
  gchar *p1 = g_file_get_path (notifier->dir);
  gchar *p2 = g_file_get_path (file);
  gboolean itself = strcmp (p1, p2) == 0;

  g_free (p1);
  g_free (p2);

  debug_print (2, "Processing notifier parent change...");

  if (event_type == G_FILE_MONITOR_EVENT_DELETED && itself)
    {
      debug_print (1, "Processing notifier dir deletion...");
      g_idle_add (notifier_go_up, notifier->browser);
    }

}

static void
notifier_changed (GFileMonitor *self, GFile *file, GFile *other_file,
		  GFileMonitorEvent event_type, gpointer user_data)
{
  struct notifier *notifier = user_data;
  gchar *p1 = g_file_get_path (notifier->dir);
  gchar *p2 = g_file_get_path (file);
  gboolean itself = strcmp (p1, p2) == 0;

  g_free (p1);
  g_free (p2);

  debug_print (2, "Processing notifier change...");

  if (event_type != G_FILE_MONITOR_EVENT_DELETED || !itself)
    {
      debug_print (1, "Processing notifier reload...");
      g_idle_add (browser_load_dir, notifier->browser);
    }
}

void
notifier_init (struct notifier **notifier, struct browser *browser)
{
  struct notifier *n = g_malloc (sizeof (struct notifier));
  n->monitor = NULL;
  n->parent_monitor = NULL;
  n->browser = browser;
  *notifier = n;
}

void
notifier_update_dir (struct notifier *notifier, gboolean active)
{
  GFile *parent;

  debug_print (1, "Changing %s browser path to '%s'...",
	       notifier->browser->name, notifier->browser->dir);

  if (notifier->monitor)
    {
      g_object_unref (notifier->monitor);
      g_object_unref (notifier->dir);
    }

  if (notifier->parent_monitor)
    {
      g_object_unref (notifier->parent_monitor);
    }

  if (active)
    {
      notifier->dir = g_file_new_for_path (notifier->browser->dir);
      notifier->monitor = g_file_monitor_directory (notifier->dir,
						    G_FILE_MONITOR_NONE, NULL,
						    NULL);
      g_file_monitor_set_rate_limit (notifier->monitor, NOTIFIER_RATE_LIMIT);
      g_signal_connect (notifier->monitor, "changed",
			G_CALLBACK (notifier_changed), notifier);

      parent = g_file_get_parent (notifier->dir);
      if (parent)
	{
	  notifier->parent_monitor = g_file_monitor_directory (parent,
							       G_FILE_MONITOR_NONE,
							       NULL, NULL);
	  g_file_monitor_set_rate_limit (notifier->parent_monitor,
					 NOTIFIER_RATE_LIMIT);
	  g_signal_connect (notifier->parent_monitor, "changed",
			    G_CALLBACK (notifier_parent_changed), notifier);
	  g_object_unref (parent);
	}
      else
	{
	  notifier->parent_monitor = NULL;
	}
    }
  else
    {
      notifier->monitor = NULL;
      notifier->parent_monitor = NULL;
    }
}

void
notifier_destroy (struct notifier *notifier)
{
  if (notifier->monitor)
    {
      g_object_unref (notifier->monitor);
      g_object_unref (notifier->dir);
    }

  if (notifier->parent_monitor)
    {
      g_object_unref (notifier->parent_monitor);
    }

  g_free (notifier);
}