teleport-window.c 6.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* teleport-window.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/>.
*/

19 20
#include <gtk/gtk.h>

21
#include "teleport-app.h"
22 23 24
#include "teleport-window.h"
#include "teleport-server.h"
#include "teleport-peer.h"
25
#include "teleport-remote-device.h"
26

27
TeleportWindow *mainWin;
28

29
struct _TeleportWindow
30 31 32 33
{
  GtkApplicationWindow parent;
};

34
typedef struct _TeleportWindowPrivate TeleportWindowPrivate;
Julian Sparber's avatar
Julian Sparber committed
35

36
struct _TeleportWindowPrivate
Julian Sparber's avatar
Julian Sparber committed
37 38 39
{
  GSettings *settings;
  GtkWidget *gears;
40
  GtkWidget *remote_devices_box;
41
  GtkWidget *this_device_name_label;
42
  GtkWidget *remote_no_devices;
Julian Sparber's avatar
Julian Sparber committed
43 44
};

45
G_DEFINE_TYPE_WITH_PRIVATE(TeleportWindow, teleport_window, GTK_TYPE_APPLICATION_WINDOW);
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
static void
change_download_directory_cb (GtkWidget *widget,
                              gpointer user_data) {
  GSettings *settings;
  gchar * newDownloadDir;
  settings = (GSettings *)user_data;

  newDownloadDir = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
  g_print ("Change download directory\n");
  g_settings_set_string (settings,
                         "download-dir",
                         newDownloadDir);
  g_free(newDownloadDir);
}

62
static void
63
teleport_window_init (TeleportWindow *win)
64
{
65
  TeleportWindowPrivate *priv;
66
  GtkBuilder *builder;
Julian Sparber's avatar
Julian Sparber committed
67
  GtkWidget *menu;
68
  GtkFileChooserButton *downloadDir;
69
  mainWin = win;
70

71
  priv = teleport_window_get_instance_private (win);
72 73 74 75 76 77 78 79 80 81 82 83 84
  priv->settings = g_settings_new ("com.frac_tion.teleport");

  if (g_settings_get_user_value (priv->settings, "download-dir") == NULL) {
    g_print ("Download dir set to XDG DOWNLOAD directory\n");
    if (g_get_user_special_dir(G_USER_DIRECTORY_DOWNLOAD) != NULL) {
      g_settings_set_string (priv->settings,
                             "download-dir",
                             g_get_user_special_dir(G_USER_DIRECTORY_DOWNLOAD));
    }
    else {
      g_print ("Error: XDG DOWNLOAD is not set.\n");
    }
  }
85

86 87 88 89 90 91
  if (g_settings_get_user_value (priv->settings, "device-name") == NULL) {
    g_settings_set_string (priv->settings,
                           "device-name",
                           g_get_host_name());
  }

92 93
  gtk_widget_init_template (GTK_WIDGET (win));

94
  builder = gtk_builder_new_from_resource ("/com/frac_tion/teleport/settings.ui");
Julian Sparber's avatar
Julian Sparber committed
95
  menu = GTK_WIDGET (gtk_builder_get_object (builder, "settings"));
96
  downloadDir = GTK_FILE_CHOOSER_BUTTON (gtk_builder_get_object (builder, "settings_download_directory"));
Julian Sparber's avatar
Julian Sparber committed
97 98

  gtk_menu_button_set_popover(GTK_MENU_BUTTON (priv->gears), menu);
99

100 101
  gtk_label_set_text (GTK_LABEL (priv->this_device_name_label),
                      g_settings_get_string (priv->settings, "device-name"));
102

103 104 105 106 107 108 109 110 111
  gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (downloadDir),
                                       g_settings_get_string(priv->settings,
                                                             "download-dir"));

  g_signal_connect (downloadDir, "file-set", G_CALLBACK (change_download_directory_cb), priv->settings);
  /*g_settings_bind (priv->settings, "download-dir",
    GTK_FILE_CHOOSER (downloadDir), "current-folder",
    G_SETTINGS_BIND_DEFAULT);
    */
112

113 114
  //g_object_unref (menu);
  //g_object_unref (label);
115 116
  g_object_unref (builder);
}
117

118

119 120
void
update_remote_device_list(TeleportWindow *win,
121
                          Peer           *device)
122 123
{
  TeleportWindowPrivate *priv;
124
  GtkWidget *remote_device;
125

126
  priv = teleport_window_get_instance_private (win);
127

128 129
  gtk_widget_hide (priv->remote_no_devices);

130
  remote_device = teleport_remote_device_new (device);
131

132 133 134 135 136
  gtk_box_pack_end (GTK_BOX (priv->remote_devices_box),
                    remote_device,
                    TRUE,
                    TRUE,
                    0);
137
}
138

139 140 141 142 143 144 145 146
static void
remove_remote_peer (GtkWidget *widget,
                    gpointer data)
{
  if (TELEPORT_IS_REMOTE_DEVICE (widget) && teleport_remote_device_get_peer(widget) == ((Peer *) data)) {
    gtk_widget_destroy (widget);
  }
}
147

148 149 150 151 152
void
update_remote_device_list_remove(TeleportWindow *win,
                                 Peer *device)
{
  TeleportWindowPrivate *priv;
153

154
  priv = teleport_window_get_instance_private (win);
155

156 157 158
  gtk_container_foreach (GTK_CONTAINER(priv->remote_devices_box),
                         remove_remote_peer,
                         device);
Julian Sparber's avatar
Julian Sparber committed
159 160
}

161
static void
162
teleport_window_dispose (GObject *object)
Julian Sparber's avatar
Julian Sparber committed
163
{
164 165
  TeleportWindow *win;
  TeleportWindowPrivate *priv;
Julian Sparber's avatar
Julian Sparber committed
166

167 168
  win = TELEPORT_WINDOW (object);
  priv = teleport_window_get_instance_private (win);
Julian Sparber's avatar
Julian Sparber committed
169

170
  g_clear_object (&priv->settings);
Julian Sparber's avatar
Julian Sparber committed
171

172
  G_OBJECT_CLASS (teleport_window_parent_class)->dispose (object);
173 174
}

175
static void
176
teleport_window_class_init (TeleportWindowClass *class)
177
{
178
  G_OBJECT_CLASS (class)->dispose = teleport_window_dispose;
Julian Sparber's avatar
Julian Sparber committed
179

180
  gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class),
181
                                               "/com/frac_tion/teleport/window.ui");
Julian Sparber's avatar
Julian Sparber committed
182

183 184 185
  gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class), TeleportWindow, gears);
  gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class), TeleportWindow, this_device_name_label);
  gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class), TeleportWindow, remote_no_devices);
186
  gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class), TeleportWindow, remote_devices_box);
187 188
}

189 190
TeleportWindow *
teleport_window_new (TeleportApp *app)
191
{
192
  return g_object_new (TELEPORT_WINDOW_TYPE, "application", app, NULL);
193 194
}

195 196 197 198 199 200 201 202
gchar * 
teleport_get_device_name (void) 
{
  TeleportWindowPrivate *priv;
  priv = teleport_window_get_instance_private (mainWin);

  return g_settings_get_string (priv->settings, "device-name");
}
203 204 205 206 207 208 209 210 211

gchar * 
teleport_get_download_directory (void) 
{
  TeleportWindowPrivate *priv;
  priv = teleport_window_get_instance_private (mainWin);

  return g_settings_get_string (priv->settings, "download-dir");
}
212

213 214 215 216 217 218 219 220
void
teleport_show_no_device_message (TeleportWindow *self)
{
  TeleportWindowPrivate *priv;
  priv = teleport_window_get_instance_private (self);
  gtk_widget_show (priv->remote_no_devices);
}

221
void
222 223
teleport_window_open (TeleportWindow *win,
                      GFile *file)
224
{
225 226
  //TeleportWindowPrivate *priv;
  //priv = teleport_window_get_instance_private (win);
227
}