~unity-team/unity/7.1

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
/*
 * Copyright 2011 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3, as
 * published by the  Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of both the GNU Lesser General Public
 * License version 3 along with this program.  If not, see
 * <http://www.gnu.org/licenses/>
 **
 * Authored by: Nick Dedekind <nick.dedekind@canonical.com>
 *
 */

#include <Nux/Nux.h>
#include "UnityCore/GLibWrapper.h"
#include "UserThumbnailProvider.h"
#include "ThumbnailGenerator.h"

namespace unity
{

namespace UserThumbnailProvider
{

class UserThumbnailer : public Thumbnailer
{
public:
  UserThumbnailer(std::string const& name, std::string const& command_line)
  : name(name)
  , command_line(command_line)
  {}

  std::string name;
  std::string command_line;

  virtual std::string GetName() const { return name; }

  virtual bool Run(int size, std::string const& input_file, std::string& output_file, std::string& error_hint);
};

bool UserThumbnailer::Run(int size, std::string const& input_file, std::string& output_file, std::string& error_hint)
{
  std::string tmp_command_line = command_line;

  // replace size
  size_t pos = tmp_command_line.find("%s");
  std::stringstream ss; ss << size;
  if (pos != std::string::npos) { tmp_command_line.replace(pos, 2, ss.str()); }

  // replace input file name
  pos = tmp_command_line.find("%u");
  if (pos != std::string::npos) { tmp_command_line.replace(pos, 2, input_file); }

  // replace output file name
  pos = tmp_command_line.find("%o");
  if (pos != std::string::npos) { tmp_command_line.replace(pos, 2, output_file); }

  gint exit_status = 0;
  GError* err = NULL;
  g_spawn_command_line_sync(tmp_command_line.c_str(), NULL, NULL, &exit_status, &err);
  if (err != NULL)
  {
    error_hint = err->message;
    g_error_free (err);
    return false;
  }
  else if (exit_status != 0)
  {
    std::stringstream ss;
    ss << "Failed to create thumbnail. Program exited with exit_status=" << exit_status;
    error_hint = ss.str();
    return false;
  }

  return true;
}

void Initialise()
{
  GError* err = NULL;
  GDir* thumbnailer_dir = g_dir_open("/usr/share/thumbnailers", 0, &err);
  if (err != NULL)
    return;

  const gchar* file;
  while((file = g_dir_read_name(thumbnailer_dir)) != NULL)
  {
    std::string file_name(file);
    if (file_name == "." || file_name == "..")
      continue;

    /*********************************
     * READ SETTINGS
     *********************************/  

    GKeyFile* key_file = g_key_file_new();

    err=NULL;
    if (!g_key_file_load_from_file (key_file, (std::string("/usr/share/thumbnailers/") + file_name).c_str(), G_KEY_FILE_NONE, &err))
    {
      g_key_file_free(key_file);
      g_error_free(err);
      continue;
    }

    err=NULL;
    glib::String command_line(g_key_file_get_string (key_file, "Thumbnailer Entry", "Exec", &err));
    if (err != NULL)
    {
      g_key_file_free(key_file);
      g_error_free(err);
      continue;
    }

    err=NULL;
    gsize mime_count = 0;
    gchar** mime_types = g_key_file_get_string_list (key_file, "Thumbnailer Entry", "MimeType", &mime_count, &err);
    if (err != NULL)
    {
      g_key_file_free(key_file);
      g_error_free(err);
      continue;
    }

    Thumbnailer::Ptr thumbnailer(new UserThumbnailer(file_name, command_line.Value()));
    std::list<std::string> mime_type_list;
    for (gsize i = 0; i < mime_count && mime_types[i] != NULL; i++)
    {
      mime_type_list.push_front(mime_types[i]);
    }

    ThumbnailGenerator::RegisterThumbnailer(mime_type_list, thumbnailer);

    g_strfreev(mime_types);
    g_key_file_free(key_file);
  }

  g_dir_close(thumbnailer_dir);
}

} // namespace DefaultThumbnailProvider
} // namespace unity