File: url.cc

package info (click to toggle)
pan 0.146-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 14,152 kB
  • sloc: cpp: 42,858; ansic: 26,234; sh: 4,711; makefile: 467; xml: 72; sed: 16
file content (117 lines) | stat: -rw-r--r-- 3,198 bytes parent folder | download | duplicates (2)
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * Pan - A Newsreader for Gtk+
 * Copyright (C) 2002-2006  Charles Kerr <charles@rebelbase.com>
 *
 * 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; version 2 of the License.
 *
 * 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
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>
#include <iostream>
#include <string>
extern "C" {
  #include <glib.h>
  #include <glib/gi18n.h>
};
#include <pan/general/log.h>
#include "url.h"

using namespace pan;

// possible values: "gnome", "kde", "custom"
const char*
URL :: get_environment ()
{
  const char * mode ("custom");
  if (g_getenv ("GNOME_DESKTOP_SESSION_ID"))
    mode = "gnome";
  else if (g_getenv ("KDE_FULL_SESSION"))
    mode = "kde";
  return mode;
}

void
URL :: get_default_editors (std::set<std::string>& editors)
{
  editors.clear ();

  const std::string environment = URL :: get_environment ();
  if (environment == "windows")
  {
    editors.insert ("notepad");
    editors.insert ("notepad2");
    editors.insert ("pfe");
  }
  else if (environment == "mac")
  {
    editors.insert ("edit -w");
    editors.insert ("TextEdit");
  }
  else if (environment == "kde")
  {
    editors.insert ("kate");
    editors.insert ("kwrite");
  }
  else // gnome and default
  {
    editors.insert ("gedit");
    editors.insert ("gvim -f");
    editors.insert ("xterm -e vim");
  }
}


void
URL :: open (const Prefs& prefs, const char * url, Mode mode)
{
  g_return_if_fail (url && *url);

  std::string tmp;

  if (mode==AUTO) {
    if (strstr(url,"mailto"))
      mode = MAIL;
    else if (strstr(url,"http") || strstr(url,"://"))
      mode = WEB;
    else // ...
      mode = WEB; 
  }
  
  if ((mode==AUTO || mode==WEB) && !strstr (url, "://") && strstr (url, "www")) {
    mode = WEB;
    tmp = std::string("http://") + url;
  } else if ((mode==AUTO || mode==MAIL) && !strstr (url, "://") && strchr (url, '@') && !strstr(url,"mailto:")) {
    mode = MAIL;
    tmp = std::string("mailto:") + url;
  } else {
    tmp = url;
  }

  const char * mode_key = mode==WEB ? "browser-mode" : "mailer-mode";
  const char * custom_key = mode==WEB ? "custom-browser" : "custom-mailer";
  const char * custom_fallback = "xdg-open";

  std::string cmd;
  const std::string env (prefs.get_string (mode_key, get_environment()));
  cmd = prefs.get_string (custom_key, custom_fallback);

  cmd += std::string(" \"") + tmp + '"';
  // std::cerr << __FILE__ << ':' << __LINE__ << " cmd [" << cmd << ']' << std::endl;
  GError * err (0);
  g_spawn_command_line_async (cmd.c_str(), &err);
  if (err != NULL) {
    Log::add_err_va (_("Error starting URL: %s (Command was: %s)"), err->message, cmd.c_str());
    g_clear_error (&err);
  }
}