teleport-app.c 14.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* teleport-app.c
 *
 * Copyright 2017 Julian Sparber <julian@sparber.com>
 *
 * Teleport is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Julian Sparber's avatar
Julian Sparber committed
17
 */
18

19 20
#include <gtk/gtk.h>

Julian Sparber's avatar
Julian Sparber committed
21 22 23 24
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

25
#include "teleport-app.h"
26 27 28 29 30 31
#include "teleport-peer.h"
#include "teleport-window.h"
#include "teleport-browser.h"
#include "teleport-publish.h"
#include "teleport-server.h"
#include "teleport-get.h"
32

33

34 35 36
static void save_file_callback          (GSimpleAction *simple,
                                         GVariant      *parameter,
                                         gpointer       user_data);
37

38 39 40
static void do_nothing_callback         (GSimpleAction *simple,
                                         GVariant      *parameter,
                                         gpointer       user_data);
41

42 43 44
static void open_file_callback          (GSimpleAction *simple,
                                         GVariant      *parameter,
                                         gpointer       user_data);
45

46 47 48 49 50

static void open_folder_callback        (GSimpleAction        *simple,
                                         GVariant             *parameter,
                                         gpointer              user_data);

Julian Sparber's avatar
Julian Sparber committed
51 52 53 54 55

static void teleport_app_show_about     (GSimpleAction        *simple,
                                         GVariant             *parameter,
                                         gpointer              user_data);

56 57 58
static void teleport_app_quit           (GSimpleAction        *simple,
                                         GVariant             *parameter,
                                         gpointer              user_data);
59

60 61 62 63
enum {
  NOTIFY_USER, NOTIFY_FINISED, N_SIGNALS
};

64 65
static GActionEntry app_entries[] =
{
66 67 68
    { "save", save_file_callback, "as", NULL, NULL },
    { "decline", do_nothing_callback, "as", NULL, NULL },
    { "do-nothing", do_nothing_callback, "as", NULL, NULL },
69
    { "open-file", open_file_callback, "as", NULL, NULL },
70
    { "open-folder", open_folder_callback, "as", NULL, NULL },
Julian Sparber's avatar
Julian Sparber committed
71
    { "about", teleport_app_show_about },
72
    { "quit",   teleport_app_quit }
73 74
};

75
static TeleportApp *mainApplication;
76
static gint signalIds [N_SIGNALS];
77

78 79 80 81 82 83 84
typedef struct
{
  GtkWidget     *window;
  TeleportPeer  *peerList;
} TeleportAppPrivate;


85
struct _TeleportApp {
86 87 88 89
  GtkApplication        parent;

  /*< private >*/
  TeleportAppPrivate    *priv;
90 91
};

92
G_DEFINE_TYPE_WITH_PRIVATE (TeleportApp, teleport_app, GTK_TYPE_APPLICATION);
93

94

95 96
static void 
save_file_callback (GSimpleAction *simple,
97 98
                    GVariant      *parameter,
                    gpointer       user_data) {
99 100 101
  teleport_get_do_downloading(g_variant_get_string (g_variant_get_child_value (parameter, 0), NULL),
                              g_variant_get_string (g_variant_get_child_value (parameter, 1), NULL),
                              g_variant_get_string (g_variant_get_child_value (parameter, 2), NULL));
102 103
}

104 105
static void 
do_nothing_callback (GSimpleAction *simple,
106 107
                     GVariant      *parameter,
                     gpointer       user_data) {
108
  g_print ("No action\n");
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
static void
open_folder_callback (GSimpleAction *simple,
                      GVariant      *parameter,
                      gpointer       user_data)
{
  GDBusProxy      *proxy;
  GVariant        *retval;
  GVariantBuilder *builder;
  const gchar     *uri;
  GError **error = NULL;
  const gchar     *path;

  path = g_strdup_printf("%s/%s",
                         g_variant_get_string (g_variant_get_child_value (parameter, 3), NULL),
                         g_variant_get_string (g_variant_get_child_value (parameter, 2), NULL));

  uri = g_filename_to_uri (path, NULL, error);
  g_debug ("Show file: %s", uri);

  proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
                                         G_DBUS_PROXY_FLAGS_NONE,
                                         NULL,
                                         "org.freedesktop.FileManager1",
                                         "/org/freedesktop/FileManager1",
                                         "org.freedesktop.FileManager1",
                                         NULL, error);

  if (!proxy) {
    g_prefix_error (error,
                    ("Connecting to org.freedesktop.FileManager1 failed: "));
  }
  else {

    builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
    g_variant_builder_add (builder, "s", uri);

    retval = g_dbus_proxy_call_sync (proxy,
                                     "ShowItems",
                                     g_variant_new ("(ass)",
                                                    builder,
                                                    ""),
                                     G_DBUS_CALL_FLAGS_NONE,
                                     -1, NULL, error);

    g_variant_builder_unref (builder);
    g_object_unref (proxy);

    if (!retval)
      {
        g_prefix_error (error, ("Calling ShowItems failed: "));
      }
    else
      g_variant_unref (retval);
  }
}


168 169
static void
open_file_callback (GSimpleAction *simple,
170 171 172
                    GVariant      *parameter,
                    gpointer       user_data)
{
173
  const gchar *path;
174
  g_print("Open file\n %s%s",
175 176
          g_variant_get_string (g_variant_get_child_value (parameter, 3), NULL),
          g_variant_get_string (g_variant_get_child_value (parameter, 2), NULL));
177

178 179 180 181 182
  path = g_strdup_printf("%s/%s",
                         g_variant_get_string (g_variant_get_child_value (parameter, 3), NULL),
                         g_variant_get_string (g_variant_get_child_value (parameter, 2), NULL));

  gtk_show_uri_on_window (NULL, g_filename_to_uri(path, NULL, NULL), GDK_CURRENT_TIME, NULL);
183 184
}

185 186
void 
create_user_notification (const char *file_name, const int file_size, const char *origin_device, GVariant *target) {
187
  GNotification *notification = g_notification_new ("Teleport");
188
  TeleportAppPrivate *priv = mainApplication->priv;
189

190
  g_notification_set_body (notification,
191
                           g_strdup_printf("%s is sending %s (%s)",
192
                                           teleport_peer_get_name_by_addr (priv->peerList, origin_device),
193 194
                                           file_name,
                                           g_format_size (file_size)));
195

196
  g_notification_set_default_action_and_target_value (notification, "app.do-nothing", target);
197 198
  g_notification_add_button_with_target_value (notification, "Decline", "app.decline", target);
  g_notification_add_button_with_target_value (notification, "Save", "app.save", target);
199
  g_notification_set_priority (notification, G_NOTIFICATION_PRIORITY_HIGH);
200
  g_application_send_notification ((GApplication *) mainApplication, NULL, notification);
201
  g_object_unref (notification);
202 203
  //the example says I have to unref it but it gives a critival error
  //https://developer.gnome.org/glib/stable/gvariant-format-strings.html
204
  //g_variant_unref (target);
205 206
}

207 208
void
create_finished_notification (const char *origin, const int filesize, const char *filename, GVariant *target) {
209
  GNotification *notification = g_notification_new ("Teleport");
210 211
  TeleportAppPrivate *priv = mainApplication->priv;

212 213
  g_notification_set_body (notification,
                           g_strdup_printf("Transfer of %s from %s is complete", 
214
                                           filename,
215
                                           teleport_peer_get_name_by_addr (priv->peerList, origin)));
216
  g_notification_set_default_action_and_target_value (notification, "app.do-nothing", target);
217
  g_notification_add_button_with_target_value (notification, "Show in folder", "app.open-folder", target);
218
  g_notification_add_button_with_target_value (notification, "Open", "app.open-file", target);
219
  g_notification_set_priority (notification, G_NOTIFICATION_PRIORITY_HIGH);
220
  g_application_send_notification ((GApplication *) mainApplication, NULL, notification);
221
  g_object_unref (notification);
222 223 224
}


225 226
gboolean
mainLoopAddPeerCallback (gpointer peer) {
227 228 229 230
  TeleportAppPrivate *priv = mainApplication->priv;
  GtkWidget *window = priv->window;

  update_remote_device_list((TeleportWindow *) window, (Peer *) peer);
231 232 233
  return G_SOURCE_REMOVE;
}

234 235
gboolean
mainLoopRemovePeerCallback (gpointer peer)  {
236 237 238 239
  TeleportAppPrivate *priv = mainApplication->priv;
  GtkWidget *window = priv->window;

  update_remote_device_list_remove((TeleportWindow *) window, (Peer *) peer);
240 241 242
  //if (teleport_peer_get_number (priv->peerList) == 0)
  if (teleport_peer_get_number (priv->peerList) == 0)
    teleport_show_no_device_message (TELEPORT_WINDOW (window));
243 244 245
  return G_SOURCE_REMOVE;
}

246 247
void
callback_add_peer (GObject *instance, Peer *peer, gpointer window) {
248
  g_idle_add(mainLoopAddPeerCallback, peer);
249 250
}

251 252
void
callback_remove_peer (GObject *instance, Peer *peer, gpointer window) {
253
  g_idle_add(mainLoopRemovePeerCallback, peer);
254 255
}

256 257
void
callback_notify_user (GObject *instance, gchar *name, gpointer window) {
258
  //create_user_notification("icon.png", 2000, "Mark's laptop");
259 260
}

261
static void
262 263
teleport_app_startup (GApplication *app) {
  TeleportAppPrivate *priv;
264 265
  GtkStyleProvider *provider;

266 267
  mainApplication = TELEPORT_APP (app);
  priv = mainApplication->priv;
268

269
  g_action_map_add_action_entries (G_ACTION_MAP (app),
270 271
                                   app_entries,
                                   G_N_ELEMENTS (app_entries),
272
                                   app);
273

274 275
  G_APPLICATION_CLASS (teleport_app_parent_class)->startup (app);

276
  /* window */
277 278
  priv->window = GTK_WIDGET (teleport_window_new (TELEPORT_APP (app)));

279 280 281 282 283 284 285 286 287 288
  /* CSS style */
  provider = GTK_STYLE_PROVIDER (gtk_css_provider_new ());
  gtk_css_provider_load_from_resource (GTK_CSS_PROVIDER (provider),
                                       "com/frac_tion/teleport/style.css");
  gtk_style_context_add_provider_for_screen (gdk_screen_get_default(),
                                             provider,
                                             GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
  g_object_unref (provider);

  /* PeerList */
289 290
  priv->peerList = g_object_new (TELEPORT_TYPE_PEER, NULL);

291
  /* Add signals */
292 293 294 295
  g_signal_connect (priv->peerList, "addpeer", (GCallback)callback_add_peer, priv->window);
  g_signal_connect (priv->peerList, "removepeer", (GCallback)callback_remove_peer, priv->window);
  g_signal_connect (app, "notify_user", (GCallback)callback_notify_user, priv->window);

296
  teleport_server_run();
297
  teleport_publish_run (teleport_get_device_name());
298 299 300 301 302 303 304 305 306 307
  teleport_browser_run_avahi_service(priv->peerList);
}

static void
teleport_app_activate (GApplication *app) {
  TeleportAppPrivate *priv;
  priv = mainApplication->priv;

  gtk_widget_show (priv->window);
  gtk_window_present (GTK_WINDOW (priv->window));
308 309
}

310
static void
311
teleport_app_finalize (GObject *object)
312
{
313
  G_OBJECT_CLASS (teleport_app_parent_class)->finalize (object);
314 315
}

316
static void
317 318
teleport_app_class_init (TeleportAppClass *class)
{
319
  G_APPLICATION_CLASS (class)->startup = teleport_app_startup;
320
  G_APPLICATION_CLASS (class)->activate = teleport_app_activate;
321 322

  G_OBJECT_CLASS (class)->finalize = teleport_app_finalize;
323 324

  signalIds[NOTIFY_USER] = g_signal_new ("notify_user",
325 326 327 328 329 330 331 332 333
                                         G_TYPE_OBJECT,
                                         G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
                                         0,
                                         NULL /* accumulator */,
                                         NULL /* accumulator data */,
                                         NULL /* C marshaller */,
                                         G_TYPE_NONE /* return_type */,
                                         1,
                                         G_TYPE_STRING);
334 335
}

Julian Sparber's avatar
Julian Sparber committed
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
static void
teleport_app_show_about (GSimpleAction *simple,
                         GVariant      *parameter,
                         gpointer       user_data)
{
  TeleportAppPrivate *priv = TELEPORT_APP (user_data)->priv;
  char *copyright;
  GDateTime *date;
  int created_year = 2017;

  static const gchar *authors[] = {
    "Julian Sparber <julian@sparber.net>",
    NULL
  };

  static const gchar *artists[] = {
    "Tobias Bernard <tbernard@gnome.org>",
    NULL
  };

  date = g_date_time_new_now_local ();

  if (g_date_time_get_year (date) <= created_year)
    {
      copyright = g_strdup_printf (("Copyright \xC2\xA9 %d "
                                     "The Teleport authors"), created_year);
    }
  else
    {
      copyright = g_strdup_printf (("Copyright \xC2\xA9 %d\xE2\x80\x93%d "
                                     "The Telport authors"), created_year, g_date_time_get_year (date));
    }

  gtk_show_about_dialog (GTK_WINDOW (priv->window),
                         "program-name", ("Teleport"),
                         "version", VERSION,
                         "copyright", copyright,
                         "license-type", GTK_LICENSE_AGPL_3_0,
                         "authors", authors,
                         "artists", artists,
                         "logo-icon-name", "com.frac_tion.teleport",
                         "translator-credits", ("translator-credits"),
                         NULL);
  g_free (copyright);
  g_date_time_unref (date);
}


384 385 386 387 388 389 390 391 392 393
static void
teleport_app_quit (GSimpleAction *simple,
                   GVariant      *parameter,
                   gpointer       user_data)
{
  TeleportAppPrivate *priv = TELEPORT_APP (user_data)->priv;

  gtk_widget_destroy (priv->window);
}

394
TeleportApp *
395 396 397
teleport_app_new (void)
{
  return g_object_new (TELEPORT_APP_TYPE,
398 399 400
                       "application-id", "com.frac_tion.teleport",
                       "flags", G_APPLICATION_HANDLES_OPEN,
                       NULL);
401
}
402 403 404 405 406 407 408 409

static void
teleport_app_init (TeleportApp *app) {
  TeleportAppPrivate *priv = teleport_app_get_instance_private (app);
  app->priv = priv;
}