teleport-get.c 5.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* teleport-get.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 21 22 23
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <libsoup/soup.h>
24
#include "teleport-app.h"
25
#include "teleport-window.h"
26
#include "teleport-get.h"
27

28 29 30 31
static int saveFile (SoupMessage *, const gchar *, const gchar *);
static gchar * getFilePath (const gchar *, const gchar *);
static int get (const gchar *, const gchar *, const gchar *, const gchar *);

32 33
static gboolean debug;

34
static void
35 36 37
finished (SoupSession *session,
          SoupMessage *msg,
          gpointer target)
38
{
39
  //GVariant *target array: {originDevice, url, filename, downloadDirectory}
40
  if ((char *) g_variant_get_string (
41
                                     g_variant_get_child_value ((GVariant *) target, 2), NULL) != NULL) {
42
    saveFile(msg,
43 44 45 46
             (char *) g_variant_get_string (
                                            g_variant_get_child_value ((GVariant *) target, 3), NULL),
             (char *) g_variant_get_string (
                                            g_variant_get_child_value ((GVariant *) target, 2), NULL));
47 48

    create_finished_notification ((char *) g_variant_get_string (
49 50 51 52 53
                                                                 g_variant_get_child_value ((GVariant *) target, 0), NULL),
                                  0,
                                  g_variant_get_string (
                                                        g_variant_get_child_value ((GVariant *) target, 2), NULL),
                                  target);
54
  }
55 56
}

57 58
static int
get (const gchar *url,
59 60 61
     const gchar *originDevice,
     const gchar *downloadDirectory,
     const gchar *outputFilename) {
62
  SoupSession *session;
63
  SoupLogger *logger = NULL;
64
  SoupMessage *msg;
65 66 67 68 69 70 71

  if (!soup_uri_new (url)) {
    g_printerr ("Could not parse '%s' as a URL\n", url);
    return (1);
  }

  session = g_object_new (SOUP_TYPE_SESSION,
72 73 74 75 76 77 78
                          SOUP_SESSION_ADD_FEATURE_BY_TYPE,
                          SOUP_TYPE_CONTENT_DECODER,
                          SOUP_SESSION_USER_AGENT,
                          "teleport ",
                          SOUP_SESSION_ACCEPT_LANGUAGE_AUTO,
                          TRUE,
                          NULL);
79 80 81 82 83 84 85 86 87 88 89

  if (debug) {
    logger = soup_logger_new (SOUP_LOGGER_LOG_BODY, -1);
    soup_session_add_feature (session, SOUP_SESSION_FEATURE (logger));
    g_object_unref (logger);
  }

  msg = soup_message_new ("GET", url);
  soup_message_set_flags (msg, SOUP_MESSAGE_NO_REDIRECT);

  g_object_ref (msg);
90
  //soup_session_queue_message (session, msg, finished, loop);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  if (outputFilename == NULL) {
    soup_session_queue_message (session, msg, NULL, NULL);
  }
  else {
    GVariantBuilder *builder;
    GVariant *target;

    builder = g_variant_builder_new (G_VARIANT_TYPE ("as"));
    g_variant_builder_add (builder, "s", originDevice);
    g_variant_builder_add (builder, "s", url);
    g_variant_builder_add (builder, "s", outputFilename);
    g_variant_builder_add (builder, "s", downloadDirectory);
    target = g_variant_new ("as", builder);
    g_variant_builder_unref (builder);

    soup_session_queue_message (session, msg, finished, target);
  }
108 109 110

  return 0;
}
111

112
static int 
113 114 115
saveFile (SoupMessage *msg,
          const gchar *outputDirectory,
          const gchar *outputFilename) {
116
  const char *name;
117
  FILE *outputFile = NULL;
118

119 120 121 122 123 124
  name = soup_message_get_uri (msg)->path;

  if (SOUP_STATUS_IS_TRANSPORT_ERROR (msg->status_code))
    g_print ("%s: %d %s\n", name, msg->status_code, msg->reason_phrase);

  if (SOUP_STATUS_IS_REDIRECTION (msg->status_code)) {
125
    g_print ("%s: %d %s\n", name, msg->status_code, msg->reason_phrase);
126
  } else if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
127
    //if there is no file name and path the page will not get saved
128
    if (outputFilename == NULL) {
129
      //g_print ("%s: Got a file offered form a other peer. Will not save anything.\n", name);
130 131
    }
    else {
132
      outputFile = fopen (getFilePath(outputDirectory, outputFilename), "w");
133

134
      if (!outputFile)
135
        g_printerr ("Error trying to create file %s.\n", outputFilename);
136

137
      if (outputFile) {
138
        fwrite (msg->response_body->data,
139 140 141
                1,
                msg->response_body->length,
                outputFile);
142

143
        fclose (outputFile);
144
      }
145 146 147 148 149
    }
  }
  return 0;
}

150

151
static gchar *
152 153
getFilePath (const gchar *outputDirectory,
             const gchar *outputFilename) {
154
  return g_strdup_printf("%s/%s", outputDirectory,
155 156 157 158 159 160 161 162
                         g_uri_escape_string(outputFilename, NULL, TRUE));
}

int
teleport_get_do_client_notify (const gchar *url) {
  get (g_strdup(url), NULL, NULL, NULL);
  g_print("Offering selected file to other machine.\n");
  return 0;
163
}
164 165

int 
166 167 168
teleport_get_do_downloading (const char *originDevice,
                             const char *url,
                             const char *filename) {
169
  const gchar *outputDirectory = teleport_get_download_directory();
170
  g_print("Downloading %s to %s\n", url, g_uri_escape_string(filename, NULL, TRUE));
171
  get (g_strdup(url), originDevice, outputDirectory, filename);
172 173
  return 0;
}