vinagre-connection.c 21.8 KB
Newer Older
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
1
2
3
4
/*
 * vinagre-connection.c
 * This file is part of vinagre
 *
5
 * Copyright (C) 2007,2008,2009 - Jonh Wendell <wendell@bani.com.br>
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
18
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
19
20
 */

21
#include <stdlib.h>
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
22
#include <glib/gi18n.h>
23
#include <gio/gio.h>
24
#include <gtk/gtk.h>
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
25

Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
26
#include "vinagre-connection.h"
27
#include "vinagre-enums.h"
28
#include "vinagre-bookmarks.h"
29
#include "vinagre-plugins-engine.h"
Jonh Wendell's avatar
Jonh Wendell committed
30
#include "vinagre-utils.h"
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
31

32
33
struct _VinagreConnectionPrivate
{
34
  gchar *protocol;
35
36
  gchar *host;
  gint   port;
37
  gchar *username;
38
  gchar *password;
39
  gchar *name;
40
  gboolean fullscreen;
41
42
43
44
45
46
47
48
};

enum
{
  PROP_0,
  PROP_PROTOCOL,
  PROP_HOST,
  PROP_PORT,
49
  PROP_USERNAME,
50
  PROP_PASSWORD,
51
  PROP_NAME,
52
  PROP_BEST_NAME,
53
  PROP_FULLSCREEN
54
55
56
};

#define VINAGRE_CONNECTION_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), VINAGRE_TYPE_CONNECTION, VinagreConnectionPrivate))
57
G_DEFINE_ABSTRACT_TYPE (VinagreConnection, vinagre_connection, G_TYPE_OBJECT);
58
59
60
61
62
63

static void
vinagre_connection_init (VinagreConnection *conn)
{
  conn->priv = G_TYPE_INSTANCE_GET_PRIVATE (conn, VINAGRE_TYPE_CONNECTION, VinagreConnectionPrivate);

64
  conn->priv->protocol = NULL;
65
66
67
  conn->priv->host = NULL;
  conn->priv->port = 0;
  conn->priv->password = NULL;
68
  conn->priv->username = NULL;
69
  conn->priv->name = NULL;
70
  conn->priv->fullscreen = FALSE;
71
72
73
74
75
76
77
}

static void
vinagre_connection_finalize (GObject *object)
{
  VinagreConnection *conn = VINAGRE_CONNECTION (object);

78
  g_free (conn->priv->protocol);
79
80
81
82
  g_free (conn->priv->host);
  g_free (conn->priv->username);
  g_free (conn->priv->password);
  g_free (conn->priv->name);
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

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

static void
vinagre_connection_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
  VinagreConnection *conn;

  g_return_if_fail (VINAGRE_IS_CONNECTION (object));

  conn = VINAGRE_CONNECTION (object);

  switch (prop_id)
    {
      case PROP_PROTOCOL:
99
	vinagre_connection_set_protocol (conn, g_value_get_string (value));
100
101
102
103
104
105
106
107
108
109
	break;

      case PROP_HOST:
	vinagre_connection_set_host (conn, g_value_get_string (value));
	break;

      case PROP_PORT:
	vinagre_connection_set_port (conn, g_value_get_int (value));
	break;

110
111
112
113
      case PROP_USERNAME:
	vinagre_connection_set_username (conn, g_value_get_string (value));
	break;

114
115
116
117
      case PROP_PASSWORD:
	vinagre_connection_set_password (conn, g_value_get_string (value));
	break;

118
119
120
121
      case PROP_FULLSCREEN:
	vinagre_connection_set_fullscreen (conn, g_value_get_boolean (value));
	break;

122
123
      case PROP_NAME:
	vinagre_connection_set_name (conn, g_value_get_string (value));
124
125
	break;

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
      default:
	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
	break;
    }
}

static void
vinagre_connection_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
  VinagreConnection *conn;

  g_return_if_fail (VINAGRE_IS_CONNECTION (object));

  conn = VINAGRE_CONNECTION (object);


  switch (prop_id)
    {
      case PROP_PROTOCOL:
145
	g_value_set_string (value, conn->priv->protocol);
146
147
148
149
150
151
152
153
154
155
	break;

      case PROP_HOST:
	g_value_set_string (value, conn->priv->host);
	break;

      case PROP_PORT:
	g_value_set_int (value, conn->priv->port);
	break;

156
157
158
159
      case PROP_USERNAME:
	g_value_set_string (value, conn->priv->username);
	break;

160
161
162
163
      case PROP_PASSWORD:
	g_value_set_string (value, conn->priv->password);
	break;

164
165
166
167
      case PROP_FULLSCREEN:
	g_value_set_boolean (value, conn->priv->fullscreen);
	break;

168
169
170
171
172
173
      case PROP_NAME:
	g_value_set_string (value, conn->priv->name);
	break;

      case PROP_BEST_NAME:
	g_value_set_string (value, vinagre_connection_get_best_name (conn));
174
175
	break;

176
177
178
179
180
181
      default:
	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
	break;
    }
}

182
183
184
static void
default_fill_writer (VinagreConnection *conn, xmlTextWriter *writer)
{
185
  if (conn->priv->protocol)
Jonh Wendell's avatar
Jonh Wendell committed
186
187
188
189
190
191
    xmlTextWriterWriteElement (writer, (const xmlChar *)"protocol", (const xmlChar *)conn->priv->protocol);
  xmlTextWriterWriteElement (writer, (const xmlChar *)"name", (const xmlChar *)conn->priv->name);
  xmlTextWriterWriteElement (writer, (const xmlChar *)"host", (const xmlChar *)conn->priv->host);
  xmlTextWriterWriteElement (writer, (const xmlChar *)"username", (const xmlChar *) (conn->priv->username ? conn->priv->username : ""));
  xmlTextWriterWriteFormatElement (writer, (const xmlChar *)"port", "%d", conn->priv->port);
  xmlTextWriterWriteFormatElement (writer, (const xmlChar *)"fullscreen", "%d", conn->priv->fullscreen);
192
193
194
195
196
197
198
199
200
201
202
203
204
}

static void
default_parse_item (VinagreConnection *conn, xmlNode *root)
{
  xmlNode *curr;
  xmlChar *s_value;

  for (curr = root->children; curr; curr = curr->next)
    {
      s_value = xmlNodeGetContent (curr);

      if (!xmlStrcmp(curr->name, (const xmlChar *)"host"))
Jonh Wendell's avatar
Jonh Wendell committed
205
	vinagre_connection_set_host (conn, (const gchar *)s_value);
206
      else if (!xmlStrcmp(curr->name, (const xmlChar *)"name"))
Jonh Wendell's avatar
Jonh Wendell committed
207
	vinagre_connection_set_name (conn, (const gchar *)s_value);
208
      else if (!xmlStrcmp(curr->name, (const xmlChar *)"username"))
Jonh Wendell's avatar
Jonh Wendell committed
209
	vinagre_connection_set_username (conn, (const gchar *)s_value);
210
      else if (!xmlStrcmp(curr->name, (const xmlChar *)"port"))
Jonh Wendell's avatar
Jonh Wendell committed
211
	vinagre_connection_set_port (conn, atoi ((const char *)s_value));
212
      else if (!xmlStrcmp(curr->name, (const xmlChar *)"fullscreen"))
Jonh Wendell's avatar
Jonh Wendell committed
213
	vinagre_connection_set_fullscreen (conn, vinagre_utils_parse_boolean ((const gchar *)s_value));
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

      xmlFree (s_value);
    }
}

static gchar *
default_get_best_name (VinagreConnection *conn)
{
  if (conn->priv->name)
    return g_strdup (conn->priv->name);

  if (conn->priv->host)
    return vinagre_connection_get_string_rep (conn, FALSE);

  return NULL;
}

231
232
233
234
235
static void
default_parse_options_widget (VinagreConnection *conn, GtkWidget *widget)
{
}

236
237
238
239
240
241
242
243
244
245
246
static void
vinagre_connection_class_init (VinagreConnectionClass *klass)
{
  GObjectClass* object_class = G_OBJECT_CLASS (klass);

  g_type_class_add_private (klass, sizeof (VinagreConnectionPrivate));

  object_class->finalize = vinagre_connection_finalize;
  object_class->set_property = vinagre_connection_set_property;
  object_class->get_property = vinagre_connection_get_property;

247
248
249
250
  klass->impl_fill_writer = default_fill_writer;
  klass->impl_parse_item = default_parse_item;
  klass->impl_get_best_name = default_get_best_name;
  klass->impl_fill_conn_from_file = NULL;
251
  klass->impl_parse_options_widget = default_parse_options_widget;
252

253
254
  g_object_class_install_property (object_class,
                                   PROP_PROTOCOL,
255
256
257
258
259
260
261
262
263
                                   g_param_spec_string ("protocol",
                                                        "protocol",
	                                                "connection protocol",
                                                        NULL,
	                                                G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT |
                                                        G_PARAM_STATIC_NICK |
                                                        G_PARAM_STATIC_NAME |
                                                        G_PARAM_STATIC_BLURB));
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283

  g_object_class_install_property (object_class,
                                   PROP_HOST,
                                   g_param_spec_string ("host",
                                                        "hostname",
	                                                "hostname or ip address of this connection",
                                                        NULL,
	                                                G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT |
                                                        G_PARAM_STATIC_NICK |
                                                        G_PARAM_STATIC_NAME |
                                                        G_PARAM_STATIC_BLURB));

  g_object_class_install_property (object_class,
                                   PROP_PORT,
                                   g_param_spec_int ("port",
                                                     "port",
	                                              "tcp/ip port of this connection",
                                                      0,
                                                      G_MAXINT,
284
                                                      0,
285
286
287
288
289
290
	                                              G_PARAM_READWRITE |
                                                      G_PARAM_CONSTRUCT |
                                                      G_PARAM_STATIC_NICK |
                                                      G_PARAM_STATIC_NAME |
                                                      G_PARAM_STATIC_BLURB));

291
  g_object_class_install_property (object_class,
Jonh Wendell's avatar
Jonh Wendell committed
292
                                   PROP_USERNAME,
293
294
295
296
297
298
299
300
301
302
                                   g_param_spec_string ("username",
                                                        "username",
	                                                "username (if any) necessary for complete this connection",
                                                        NULL,
	                                                G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT |
                                                        G_PARAM_STATIC_NICK |
                                                        G_PARAM_STATIC_NAME |
                                                        G_PARAM_STATIC_BLURB));

303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
  g_object_class_install_property (object_class,
                                   PROP_PASSWORD,
                                   g_param_spec_string ("password",
                                                        "password",
	                                                "password (if any) necessary for complete this connection",
                                                        NULL,
	                                                G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT |
                                                        G_PARAM_STATIC_NICK |
                                                        G_PARAM_STATIC_NAME |
                                                        G_PARAM_STATIC_BLURB));

  g_object_class_install_property (object_class,
                                   PROP_NAME,
                                   g_param_spec_string ("name",
                                                        "connection name",
	                                                "friendly name for this connection",
                                                        NULL,
	                                                G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT |
                                                        G_PARAM_STATIC_NICK |
                                                        G_PARAM_STATIC_NAME |
                                                        G_PARAM_STATIC_BLURB));

  g_object_class_install_property (object_class,
                                   PROP_BEST_NAME,
                                   g_param_spec_string ("best-name",
                                                        "best-name",
	                                                "preferred name for this connection",
                                                        NULL,
	                                                G_PARAM_READABLE |
                                                        G_PARAM_STATIC_NICK |
                                                        G_PARAM_STATIC_NAME |
                                                        G_PARAM_STATIC_BLURB));

338
339
340
341
342
343
344
345
346
347
348
  g_object_class_install_property (object_class,
                                   PROP_FULLSCREEN,
                                   g_param_spec_boolean ("fullscreen",
                                                        "Full screen connection",
	                                                "Whether this connection is a view-only one",
                                                        FALSE,
	                                                G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT |
                                                        G_PARAM_STATIC_NICK |
                                                        G_PARAM_STATIC_NAME |
                                                        G_PARAM_STATIC_BLURB));
349
350

}
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
351

352
353
void
vinagre_connection_set_protocol (VinagreConnection *conn,
354
			         const gchar       *protocol)
355
356
{
  g_return_if_fail (VINAGRE_IS_CONNECTION (conn));
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
357

358
359
  g_free (conn->priv->protocol);
  conn->priv->protocol = g_strdup (protocol);
360
}
361
const gchar *
362
vinagre_connection_get_protocol (VinagreConnection *conn)
363
364
365
{
  g_return_val_if_fail (VINAGRE_IS_CONNECTION (conn), NULL);

366
  return conn->priv->protocol;
367
368
}

369
void
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
370
vinagre_connection_set_host (VinagreConnection *conn,
371
			     const gchar *host)
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
372
{
373
374
  g_return_if_fail (VINAGRE_IS_CONNECTION (conn));

375
  g_free (conn->priv->host);
376
377
378
379
  if (host)
    conn->priv->host = g_strdup (g_strstrip ((gchar *)host));
  else
    conn->priv->host = NULL;
380
381
382
383
384
385
386
}
const gchar *
vinagre_connection_get_host (VinagreConnection *conn)
{
  g_return_val_if_fail (VINAGRE_IS_CONNECTION (conn), NULL);

  return conn->priv->host;
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
387
388
389
390
}

void
vinagre_connection_set_port (VinagreConnection *conn,
391
			     gint port)
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
392
{
393
394
395
396
397
398
399
400
401
402
  g_return_if_fail (VINAGRE_IS_CONNECTION (conn));

  conn->priv->port = port;
}
gint
vinagre_connection_get_port (VinagreConnection *conn)
{
  g_return_val_if_fail (VINAGRE_IS_CONNECTION (conn), 0);

  return conn->priv->port;
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
403
404
}

405
406
void
vinagre_connection_set_username (VinagreConnection *conn,
407
				 const gchar *username)
408
409
410
411
412
413
414
415
416
417
418
419
420
421
{
  g_return_if_fail (VINAGRE_IS_CONNECTION (conn));

  g_free (conn->priv->username);
  conn->priv->username = g_strdup (username);
}
const gchar *
vinagre_connection_get_username (VinagreConnection *conn)
{
  g_return_val_if_fail (VINAGRE_IS_CONNECTION (conn), NULL);

  return conn->priv->username;
}

Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
422
423
void
vinagre_connection_set_password (VinagreConnection *conn,
424
			         const gchar *password)
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
425
{
426
  g_return_if_fail (VINAGRE_IS_CONNECTION (conn));
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
427

428
  g_free (conn->priv->password);
429
  conn->priv->password = g_strdup (password);
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
430
}
431
432
const gchar *
vinagre_connection_get_password (VinagreConnection *conn)
433
{
434
435
436
  g_return_val_if_fail (VINAGRE_IS_CONNECTION (conn), NULL);

  return conn->priv->password;
437
438
}

Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
439
void
440
441
vinagre_connection_set_fullscreen (VinagreConnection *conn,
				  gboolean value)
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
442
{
443
  g_return_if_fail (VINAGRE_IS_CONNECTION (conn));
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
444

445
  conn->priv->fullscreen = value;
446
}
447
448
gboolean
vinagre_connection_get_fullscreen (VinagreConnection *conn)
449
{
450
  g_return_val_if_fail (VINAGRE_IS_CONNECTION (conn), FALSE);
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
451

452
  return conn->priv->fullscreen;
453
}
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
454

455
void
456
457
vinagre_connection_set_name (VinagreConnection *conn,
			     const gchar *name)
458
459
{
  g_return_if_fail (VINAGRE_IS_CONNECTION (conn));
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
460

461
462
  g_free (conn->priv->name);
  conn->priv->name = g_strdup (name);
463
464
}
const gchar *
465
vinagre_connection_get_name (VinagreConnection *conn)
466
467
{
  g_return_val_if_fail (VINAGRE_IS_CONNECTION (conn), NULL);
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
468

469
  return conn->priv->name;
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
470
471
}

472
473
474
475
476
477
478
479
480
481
482
483
484
/**
 * vinagre_connection_split_string:
 * @uri: The URI to be splitted.
 * @known_protocol: The protocol, if it's known. NULL otherwise.
 * @protocol: Will hold the protocol of this URI
 * @host: Will hold the host of this URI
 * @port: Will hold the port of this URI
 * @error_msg: Will hold an error message in case of fail
 *
 * Splits a URI into its several parts.
 *
 * Returns: %TRUE if the URI is splitted successfuly. FALSE otherwise.
 */
485
486
gboolean
vinagre_connection_split_string (const gchar *uri,
487
				 const gchar *known_protocol,
488
489
490
491
				 gchar       **protocol,
				 gchar       **host,
				 gint         *port,
				 gchar       **error_msg)
492
{
493
  gchar **server, **url;
494
495
  gint    lport;
  gchar  *lhost;
496
  gchar   ipv6_host[255] = {0,};
Jonh Wendell's avatar
Jonh Wendell committed
497
  VinagreProtocol *ext;
498

499
  *error_msg = NULL;
500
501
  *host = NULL;
  *port = 0;
502
503
504
505

  url = g_strsplit (uri, "://", 2);
  if (g_strv_length (url) == 2)
    {
506
507
508
509
      if (known_protocol)
	*protocol = g_strdup (known_protocol);
      else
	*protocol = g_strdup (url[0]);
510
      lhost = url[1];
511
512
    }
  else
513
    {
514
515
516
517
      if (known_protocol)
	*protocol = g_strdup (known_protocol);
      else
	*protocol = g_strdup ("vnc");
518
519
      lhost = (gchar *) uri;
    }
520

Jonh Wendell's avatar
Jonh Wendell committed
521
522
523
  ext = vinagre_plugins_engine_get_plugin_by_protocol (vinagre_plugins_engine_get_default (),
						       *protocol);
  if (!ext)
524
525
526
    {
      *error_msg = g_strdup_printf (_("The protocol %s is not supported."), *protocol);
      g_free (*protocol);
527
      *protocol = NULL;
528
529
530
531
      g_strfreev (url);
      return FALSE;
    }

532
533
534
535
536
537
538
539
540
541
542
543
  if (lhost[0] == '[')
    {
      int i;
      for (i = 1; lhost[i] && lhost[i] != ']'; i++)
	{
	  ipv6_host[i-1] = lhost[i];
	  lhost[i-1] = '_';
	}
      ipv6_host[i-1] = '\0';
      lhost[i] = '_';
    }

544
  if (g_strrstr (lhost, "::") != NULL)
545
    {
546
      server = g_strsplit (lhost, "::", 2);
Jonh Wendell's avatar
Jonh Wendell committed
547
      lport = server[1] ? atoi (server[1]) : vinagre_protocol_get_default_port (ext);
548
549
550
    }
  else
    {
551
      server = g_strsplit (lhost, ":", 2);
Jonh Wendell's avatar
Jonh Wendell committed
552
      lport = server[1] ? atoi (server[1]) : vinagre_protocol_get_default_port (ext);
553

554
      if ((g_str_equal (*protocol, "vnc")) && (lport < 1024))
555
        lport += 5900;
556
557
    }

558
  lhost = ipv6_host[0] ? ipv6_host : (server[0][0] ? server[0] : "localhost");
559
560
561
562
563
564
565
566
567
568
569

  *host = g_strdup (lhost);
  *port = lport;

  g_strfreev (server);
  g_strfreev (url);

  return TRUE;
}

VinagreConnection *
570
vinagre_connection_new_from_string (const gchar *uri, gchar **error_msg, gboolean use_bookmarks)
571
{
572
  VinagreConnection *conn = NULL;
573
  gint    port;
574
  gchar  *host, *protocol;
Jonh Wendell's avatar
Jonh Wendell committed
575
  VinagreProtocol *ext;
576

577
  if (!vinagre_connection_split_string (uri, NULL, &protocol, &host, &port, error_msg))
578
    return NULL;
579

580
581
  if (use_bookmarks)
    conn = vinagre_bookmarks_exists (vinagre_bookmarks_get_default (),
582
				     protocol,
583
584
				     host,
				     port);
585
586
  if (!conn)
    {
Jonh Wendell's avatar
Jonh Wendell committed
587
588
589
      ext = vinagre_plugins_engine_get_plugin_by_protocol (vinagre_plugins_engine_get_default (),
							   protocol);
      if (!ext)
590
591
	goto finalize;

Jonh Wendell's avatar
Jonh Wendell committed
592
      conn = vinagre_protocol_new_connection (ext);
593
594
595
596
      vinagre_connection_set_host (conn, host);
      vinagre_connection_set_port (conn, port);
    }

597
finalize:
598
  g_free (host);
599
  g_free (protocol);
600
601
  return conn;
}
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
602

603
VinagreConnection *
604
vinagre_connection_new_from_file (const gchar *uri, gchar **error_msg, gboolean use_bookmarks)
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
605
{
606
  VinagreConnection *conn;
607
  gchar             *data;
608
  GFile             *file_a;
609
  GError            *error;
Jonh Wendell's avatar
Jonh Wendell committed
610
  GHashTable        *extensions;
611
  GHashTableIter     iter;
Jonh Wendell's avatar
Jonh Wendell committed
612
  gpointer           ext;
613

614
  *error_msg = NULL;
615
616
617
  data = NULL;
  conn = NULL;
  error = NULL;
618

619
  file_a = g_file_new_for_commandline_arg (uri);
620
621
622
623
624
  if (!g_file_load_contents (file_a,
			     NULL,
			     &data,
			     NULL,
			     NULL,
625
			     &error))
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
626
    {
627
628
629
630
631
      if (error)
	{
	  *error_msg = g_strdup (error->message);
	  g_error_free (error);
	}
632
633
      else
	*error_msg = g_strdup (_("Could not open the file."));
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
634

635
      goto the_end;
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
636
637
    }

Jonh Wendell's avatar
Jonh Wendell committed
638
639
640
  extensions = vinagre_plugins_engine_get_plugins_by_protocol (vinagre_plugins_engine_get_default ());
  g_hash_table_iter_init (&iter, extensions);
  while (g_hash_table_iter_next (&iter, NULL, &ext))
641
    {
Jonh Wendell's avatar
Jonh Wendell committed
642
643
644
645
      conn = vinagre_protocol_new_connection_from_file ((VinagreProtocol *)ext,
							data,
							use_bookmarks,
							error_msg);
646
647
648
649
      g_free (*error_msg);
      *error_msg = NULL;
      if (conn)
	break;
650
    }
Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
651

652
653
the_end:
  g_free (data);
654
  g_object_unref (file_a);
655

656
  if (!conn && !*error_msg)
657
    *error_msg = g_strdup (_("The file was not recognized by any of the plugins."));
658

Jonh Wendell S. Santana's avatar
Jonh Wendell S. Santana committed
659
660
  return conn;
}
661

662
663
664
665
666
667
gchar*
vinagre_connection_get_string_rep (VinagreConnection *conn,
				   gboolean has_protocol)
{
  GString *uri;
  gchar *result;
668
  gboolean is_ipv6;
Jonh Wendell's avatar
Jonh Wendell committed
669
  VinagreProtocol *ext;
670
671
672

  g_return_val_if_fail (VINAGRE_IS_CONNECTION (conn), NULL);

673
674
  is_ipv6 = g_strstr_len (conn->priv->host, -1, ":") != NULL;

675
676
  if (has_protocol)
    {
677
      uri = g_string_new (conn->priv->protocol);
678
      g_string_append (uri, "://");
679
680
681
682
    }
  else
    uri = g_string_new (NULL);

683
684
  if (is_ipv6)
    g_string_append_c (uri, '[');
685
  g_string_append (uri, conn->priv->host);
686
687
  if (is_ipv6)
    g_string_append_c (uri, ']');
688

Jonh Wendell's avatar
Jonh Wendell committed
689
690
  ext = vinagre_plugins_engine_get_plugin_by_protocol (vinagre_plugins_engine_get_default (), conn->priv->protocol);
  if (ext)
Jonh Wendell's avatar
Jonh Wendell committed
691
    if (vinagre_protocol_get_default_port (ext) != conn->priv->port)
692
      g_string_append_printf (uri, "::%d", conn->priv->port);
693

694
695
  result = uri->str;
  g_string_free (uri, FALSE);
696
697
698
699

  return result;
}

700
701
void
vinagre_connection_fill_writer (VinagreConnection *conn,
702
				xmlTextWriterPtr   writer)
703
{
Jonh Wendell's avatar
Jonh Wendell committed
704
705
  g_return_if_fail (VINAGRE_IS_CONNECTION (conn));

706
707
708
709
710
  VINAGRE_CONNECTION_GET_CLASS (conn)->impl_fill_writer (conn, writer);
}

void
vinagre_connection_parse_item (VinagreConnection *conn,
711
			       xmlNode           *root)
712
{
Jonh Wendell's avatar
Jonh Wendell committed
713
714
  g_return_if_fail (VINAGRE_IS_CONNECTION (conn));

715
716
717
718
719
720
  VINAGRE_CONNECTION_GET_CLASS (conn)->impl_parse_item (conn, root);
}

gchar*
vinagre_connection_get_best_name (VinagreConnection *conn)
{
Jonh Wendell's avatar
Jonh Wendell committed
721
722
  g_return_val_if_fail (VINAGRE_IS_CONNECTION (conn), NULL);

723
724
  return VINAGRE_CONNECTION_GET_CLASS (conn)->impl_get_best_name (conn);
}
725
726
727
728
729

void
vinagre_connection_parse_options_widget (VinagreConnection *conn,
					 GtkWidget         *widget)
{
730
731
732
  g_return_if_fail (VINAGRE_IS_CONNECTION (conn));
  g_return_if_fail (GTK_IS_WIDGET (widget));

733
734
  VINAGRE_CONNECTION_GET_CLASS (conn)->impl_parse_options_widget (conn, widget);
}
735
/* vim: set ts=8: */