~larryprice/libertine/recurse-apps-dir

« back to all changes in this revision

Viewing changes to liblibertine/libertine.cpp

  • Committer: Larry Price
  • Date: 2016-08-02 16:50:18 UTC
  • Revision ID: larry.price@canonical.com-20160802165018-0hpto8pyp9u5e01l
Add liblibertine method to find app desktop files

Show diffs side-by-side

added added

removed removed

Lines of Context:
66
66
  g_dir_close(dir);
67
67
  return nullptr;
68
68
}
69
 
}
 
69
 
 
70
 
 
71
gchar*
 
72
find_desktop_file(const gchar* path, const gchar* app_id)
 
73
{
 
74
  gchar* file_path = g_build_filename(path, app_id, nullptr);
 
75
  gchar* full_filename = g_strdup_printf("%s%s", file_path, DESKTOP_EXTENSION);
 
76
  g_free(file_path);
 
77
  gboolean exists = g_file_test(full_filename, G_FILE_TEST_IS_REGULAR);
 
78
  if (exists)
 
79
  {
 
80
    return full_filename;
 
81
  }
 
82
  g_free(full_filename);
 
83
 
 
84
  GError* error = nullptr;
 
85
  GDir* dir = g_dir_open(path, 0, &error);
 
86
  if (error != nullptr)
 
87
  {
 
88
    g_error_free(error);
 
89
    return nullptr;
 
90
  }
 
91
 
 
92
  const gchar* files;
 
93
  while ((files = g_dir_read_name(dir)) != nullptr)
 
94
  {
 
95
    gchar* file = g_build_filename(path, files, nullptr);
 
96
    if (g_file_test(file, G_FILE_TEST_IS_DIR))
 
97
    {
 
98
      full_filename = find_desktop_file(file, app_id);
 
99
      if (full_filename != nullptr)
 
100
      {
 
101
        g_free(file);
 
102
        g_free(dir);
 
103
        return full_filename;
 
104
      }
 
105
    }
 
106
    g_free(file);
 
107
  }
 
108
 
 
109
  g_free(dir);
 
110
 
 
111
  return nullptr;
 
112
}
 
113
 
 
114
 
 
115
gchar*
 
116
find_app_info(gchar* primary_path, const gchar* secondary_path, const gchar* app_id)
 
117
{
 
118
  auto path = g_build_filename("/", primary_path, secondary_path, nullptr);
 
119
  gchar* desktop_file = find_desktop_file(path, app_id);
 
120
  g_free(path);
 
121
  return desktop_file;
 
122
}
 
123
}
 
124
 
70
125
 
71
126
gchar**
72
127
libertine_list_apps_for_container(const gchar* container_id)
102
157
  return (gchar**)g_array_free(apps, FALSE);
103
158
}
104
159
 
 
160
 
 
161
gchar*
 
162
libertine_find_desktop(const gchar* app_id, const gchar* container_id)
 
163
{
 
164
  g_return_val_if_fail(app_id != nullptr && container_id != nullptr, nullptr);
 
165
 
 
166
  gchar* base_path = libertine_container_path(container_id);
 
167
  g_return_val_if_fail(base_path != nullptr, nullptr);
 
168
 
 
169
  // check global path
 
170
  auto desktop_file = find_app_info(base_path, GLOBAL_APPLICATIONS, app_id);
 
171
  g_free(base_path);
 
172
  if (desktop_file != nullptr)
 
173
  {
 
174
    return desktop_file;
 
175
  }
 
176
 
 
177
  // check local path
 
178
  auto home_path = libertine_container_home_path(container_id);
 
179
  desktop_file = find_app_info(home_path, LOCAL_APPLICATIONS, app_id);
 
180
  g_free(home_path);
 
181
 
 
182
  return desktop_file;
 
183
}
 
184
 
 
185
 
105
186
gchar **
106
187
libertine_list_containers(void)
107
188
{