summaryrefslogtreecommitdiff
path: root/ytstenut/yts-file-transfer.c
blob: 9c105c193d4ae4123d5872b750761eb68863cecc (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright © 2011 Intel Corp.
 *
 * This  library is free  software; you can  redistribute it and/or
 * modify it  under  the terms  of the  GNU Lesser  General  Public
 * License  as published  by the Free  Software  Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Authored by: Rob Staudinger <robsta@linux.intel.com>
 */

#include <stdbool.h>

#include "yts-file-transfer.h"
#include "yts-marshal.h"

/* HACK, include known implementers headers for type checks. */
#include "yts-incoming-file-internal.h"
#include "yts-outgoing-file-internal.h"

#include "config.h"

G_DEFINE_INTERFACE (YtsFileTransfer, yts_file_transfer, G_TYPE_OBJECT)

/**
 * SECTION: yts-file-transfer
 * @short_description: Common interface for file transfers between Ytstenut
 * services.
 *
 * #YtsFileTransfer represents an ongoing file transfer operation between
 * Ytstenut services, and offers progress- and status-reporting facilities.
 */

enum {
  SIG_CANCELLED = 0,
  SIG_ERROR,

  LAST_SIGNAL
};

static unsigned _signals[LAST_SIGNAL] = { 0, };

static void
yts_file_transfer_default_init (YtsFileTransferInterface *interface)
{
  static bool _initialized = false;

  if (!_initialized) {

    GParamSpec *pspec;

    /**
     * YtsFileTransfer:file:
     *
     * The #GFile instance backing the transfer.
     *
     * Since: 0.4
     */
    pspec = g_param_spec_object ("file", "", "",
                                 G_TYPE_FILE,
                                 G_PARAM_READWRITE |
                                 G_PARAM_CONSTRUCT_ONLY |
                                 G_PARAM_STATIC_STRINGS);
    g_object_interface_install_property (interface, pspec);

    /**
     * YtsFileTransfer:progress:
     *
     * Read-only property that holds the file transmission progress. Values range
     * from 0.0 at the start of the transfer, to 1.0 upon completion.
     * Error or cancellation leaves the progress with a value smaller than zero.
     */
    pspec = g_param_spec_float ("progress", "", "",
                                -0.1, 1.1, 0.0,
                                G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
    g_object_interface_install_property (interface, pspec);

    /**
     * YtsFileTransfer::error:
     * @self: object which emitted the signal.
     * @error: the #GError causing the transmission to fail.
     *
     * This signal is emitted when the transmission failed, transporting
     * error details.
     *
     * Since: 0.4
     */
    _signals[SIG_ERROR] = g_signal_new ("error",
                                        G_TYPE_FROM_INTERFACE (interface),
                                        G_SIGNAL_RUN_LAST,
                                        0, NULL, NULL,
                                        yts_marshal_VOID__BOXED,
                                        G_TYPE_NONE, 1,
                                        G_TYPE_ERROR);

    /**
     * YtsFileTransfer::cancelled:
     * @self: object which emitted the signal.
     *
     * This signal is emitted when remote peer cancelled the file transmission.
     *
     * Since: 0.4
     */
    _signals[SIG_CANCELLED] = g_signal_new ("cancelled",
                                            G_TYPE_FROM_INTERFACE (interface),
                                            G_SIGNAL_RUN_LAST,
                                            0, NULL, NULL,
                                            yts_marshal_VOID__VOID,
                                            G_TYPE_NONE, 0);
  }
}

/**
 * yts_file_transfer_get_file:
 * @self: object on which to invoke this method.
 *
 * See YtsFileTransfer:file property for details.
 *
 * Returns: #GFile instance backing the transfer.
 */
GFile *const
yts_file_transfer_get_file (YtsFileTransfer *self)
{
  /* Known subclasses, so hack it up for const return. */

  if (YTS_IS_INCOMING_FILE (self)) {

    return yts_incoming_file_get_file (YTS_INCOMING_FILE (self));

  } else if (YTS_IS_OUTGOING_FILE (self)) {

    return yts_outgoing_file_get_file (YTS_OUTGOING_FILE (self));

  } else {

    g_warning ("Unhandled YtsFileTransfer instance %s in %s",
               G_OBJECT_TYPE_NAME (self),
               __FUNCTION__);
  }

  return NULL;
}

/**
 * yts_file_transfer_get_progress:
 * @self: object on which to invoke this method.
 *
 * Get progress of file transfer operation, see #YtsFileTransfer:progress for
 * details about the range of values.
 *
 * Returns: file transfer progress.
 */
float
yts_file_transfer_get_progress (YtsFileTransfer *self)
{
  float progress;

  g_return_val_if_fail (YTS_IS_FILE_TRANSFER (self), -1.0);

  g_object_get (self, "progress", &progress, NULL);

  return progress;
}