file-monitor.c 4.46 KB
Newer Older
1
/* SPDX-License-Identifier: Zlib */
2 3 4

#include "file-monitor.h"
#include "file-monitor-glib.h"
5
#ifdef G_OS_UNIX
6
#include "file-monitor-signal.h"
7
#endif
8
#include "file-monitor-noop.h"
9 10
#include "macros.h"

11 12
#include <girara/utils.h>

13 14
typedef struct private_s {
  char* file_path;
Sebastian Ramacher's avatar
Sebastian Ramacher committed
15
} ZathuraFileMonitorPrivate;
16

Sebastian Ramacher's avatar
Sebastian Ramacher committed
17
G_DEFINE_TYPE_WITH_CODE(ZathuraFileMonitor, zathura_filemonitor, G_TYPE_OBJECT, G_ADD_PRIVATE(ZathuraFileMonitor))
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

enum {
  PROP_0,
  PROP_FILE_PATH
};

enum {
  RELOAD_FILE,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

static void
finalize(GObject* object)
{
  ZathuraFileMonitor* file_monitor = ZATHURA_FILEMONITOR(object);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
35
  ZathuraFileMonitorPrivate* private               = zathura_filemonitor_get_instance_private(file_monitor);
36 37 38 39 40 41 42 43 44 45 46 47

  if (private->file_path != NULL) {
    g_free(private->file_path);
  }

  G_OBJECT_CLASS(zathura_filemonitor_parent_class)->finalize(object);
}

static void
set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec)
{
  ZathuraFileMonitor* file_monitor = ZATHURA_FILEMONITOR(object);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
48
  ZathuraFileMonitorPrivate* private               = zathura_filemonitor_get_instance_private(file_monitor);
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

  switch (prop_id) {
    case PROP_FILE_PATH:
      if (private->file_path != NULL) {
        g_free(private->file_path);
      }
      private->file_path = g_value_dup_string(value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
  }
}

static void
get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec)
{
  ZathuraFileMonitor* file_monitor = ZATHURA_FILEMONITOR(object);
Sebastian Ramacher's avatar
Sebastian Ramacher committed
66
  ZathuraFileMonitorPrivate* private               = zathura_filemonitor_get_instance_private(file_monitor);
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

  switch (prop_id) {
    case PROP_FILE_PATH:
      g_value_set_string(value, private->file_path);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
  }
}

static void
zathura_filemonitor_class_init(ZathuraFileMonitorClass* class)
{
  /* set up methods */
  class->start = NULL;
  class->stop  = NULL;

  GObjectClass* object_class = G_OBJECT_CLASS(class);
  object_class->finalize     = finalize;
  object_class->set_property = set_property;
  object_class->get_property = get_property;

  /* add properties */
  g_object_class_install_property(
    object_class, PROP_FILE_PATH,
    g_param_spec_string("file-path", "file-path", "file path to monitor", NULL,
                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
                          G_PARAM_STATIC_STRINGS));

  /* add signals */
  signals[RELOAD_FILE] =
    g_signal_new("reload-file", ZATHURA_TYPE_FILEMONITOR, G_SIGNAL_RUN_LAST, 0,
                 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 0);
}

static void
zathura_filemonitor_init(ZathuraFileMonitor* file_monitor)
{
Sebastian Ramacher's avatar
Sebastian Ramacher committed
105 106
  ZathuraFileMonitorPrivate* private = zathura_filemonitor_get_instance_private(file_monitor);
  private->file_path                 = NULL;
107 108
}

Sebastian Ramacher's avatar
Sebastian Ramacher committed
109
const char* zathura_filemonitor_get_filepath(ZathuraFileMonitor* file_monitor)
110
{
Sebastian Ramacher's avatar
Sebastian Ramacher committed
111
  ZathuraFileMonitorPrivate* private = zathura_filemonitor_get_instance_private(file_monitor);
112 113 114
  return private->file_path;
}

115 116 117 118 119 120 121 122 123 124
void zathura_filemonitor_start(ZathuraFileMonitor* file_monitor)
{
  ZATHURA_FILEMONITOR_GET_CLASS(file_monitor)->start(file_monitor);
}

void zathura_filemonitor_stop(ZathuraFileMonitor* file_monitor)
{
  ZATHURA_FILEMONITOR_GET_CLASS(file_monitor)->stop(file_monitor);
}

125 126 127 128 129 130 131 132 133
ZathuraFileMonitor*
zathura_filemonitor_new(const char*                file_path,
                        zathura_filemonitor_type_t filemonitor_type)
{
  g_return_val_if_fail(file_path != NULL, NULL);

  GObject* ret = NULL;
  switch (filemonitor_type) {
    case ZATHURA_FILEMONITOR_GLIB:
Sebastian Ramacher's avatar
Sebastian Ramacher committed
134
      girara_debug("using glib file monitor");
135 136 137
      ret = g_object_new(ZATHURA_TYPE_GLIBFILEMONITOR, "file-path", file_path,
                         NULL);
      break;
138
#ifdef G_OS_UNIX
139
    case ZATHURA_FILEMONITOR_SIGNAL:
Sebastian Ramacher's avatar
Sebastian Ramacher committed
140
      girara_debug("using SIGHUP file monitor");
141 142 143
      ret = g_object_new(ZATHURA_TYPE_SIGNALFILEMONITOR, "file-path", file_path,
                         NULL);
      break;
144
#endif
145 146 147 148 149
    case ZATHURA_FILEMONITOR_NOOP:
      girara_debug("using noop file monitor");
      ret = g_object_new(ZATHURA_TYPE_NOOPFILEMONITOR, "file-path", file_path,
                         NULL);
      break;
150
    default:
151
      girara_debug("invalid filemonitor type: %d", filemonitor_type);
152 153 154 155 156 157 158
      g_return_val_if_fail(false, NULL);
  }

  if (ret == NULL) {
    return NULL;
  }

159
  return ZATHURA_FILEMONITOR(ret);
160 161
}