README: adjust the jackdbus homepage URL
[ladish.git] / gui / load_project_dialog.c
blob16edd119c37a749670c5f37dae2df24fa3c98097
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*
3 * LADI Session Handler (ladish)
5 * Copyright (C) 2010,2012 Nedko Arnaudov <nedko@arnaudov.name>
7 **************************************************************************
8 * This file contains implementation of the load project dialog
9 **************************************************************************
11 * LADI Session Handler is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * LADI Session Handler is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
23 * or write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/stat.h>
29 #include "load_project_dialog.h"
30 #include "gtk_builder.h"
31 #include "../common/catdup.h"
33 enum
35 COL_NAME = 0,
36 COL_MODIFIED,
37 COL_DESCRIPTION,
38 COL_MTIME,
39 NUM_COLS
42 #if 0
43 static
44 void
45 convert_timestamp_to_string(
46 const time_t timestamp,
47 std::string& timestamp_string)
49 GDate mtime, now;
50 gint days_diff;
51 struct tm tm_mtime;
52 time_t time_now;
53 const gchar *format;
54 gchar buf[256];
56 if (timestamp == 0)
58 timestamp_string = "Unknown";
59 return;
62 localtime_r(&timestamp, &tm_mtime);
64 g_date_set_time_t(&mtime, timestamp);
65 time_now = time(NULL);
66 g_date_set_time_t(&now, time_now);
68 days_diff = g_date_get_julian(&now) - g_date_get_julian(&mtime);
70 if (days_diff == 0)
72 format = "Today at %H:%M";
74 else if (days_diff == 1)
76 format = "Yesterday at %H:%M";
78 else
80 if (days_diff > 1 && days_diff < 7)
82 format = "%A"; /* Days from last week */
84 else
86 format = "%x"; /* Any other date */
90 if (strftime(buf, sizeof(buf), format, &tm_mtime) != 0)
92 timestamp_string = buf;
94 else
96 timestamp_string = "Unknown";
99 #endif
101 #if 0
102 static int mtime_sorter(GtkTreeModel * model, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data)
104 uint64_t ta;
105 uint64_t tb;
107 gtk_tree_model_get(GTK_TREE_MODEL(user_data), a, COL_MTIME, &ta, -1);
108 gtk_tree_model_get(GTK_TREE_MODEL(user_data), b, COL_MTIME, &tb, -1);
110 return ta > tb ? -1 : (ta == tb ? 0 : 1);
112 #endif
114 #if 0
115 static gboolean reject_filter(const GtkFileFilterInfo * UNUSED(filter_info), gpointer UNUSED(data))
117 //log_info("filter: '%s'", filter_info->filename);
118 return FALSE;
120 #endif
122 static bool is_project_dir(const char * dir)
124 char * file;
125 struct stat st;
126 int ret;
128 file = catdup(dir, "/ladish-project.xml");
129 if (file == NULL)
131 log_error("catdup() failed to compose project file path");
132 return false;
135 ret = stat(file, &st);
136 free(file);
138 return ret == 0;
141 #if 0
142 static void on_dir_select(GtkWidget * widget, gpointer data)
144 char * dir;
145 gboolean sensitive;
147 //log_info("selection-changed signal");
149 dir = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(data));
150 sensitive = FALSE;
151 if (dir != NULL)
153 sensitive = is_project_dir(dir);
154 g_free(dir);
157 gtk_widget_set_sensitive(gtk_dialog_get_widget_for_response(GTK_DIALOG(data), GTK_RESPONSE_ACCEPT), sensitive);
159 #endif
161 static void dir_changed(GtkWidget * UNUSED(widget), gpointer data)
163 char * dir;
165 //log_info("current-folder-changed signal");
167 dir = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(data));
168 if (dir == NULL)
170 return;
173 //log_info("dir changed: '%s'", dir);
175 if (is_project_dir(dir))
177 gtk_widget_activate(gtk_dialog_get_widget_for_response(GTK_DIALOG(data), GTK_RESPONSE_ACCEPT));
181 //static void on_browse(GtkWidget * widget, gpointer data)
182 void ladish_run_load_project_dialog(ladish_room_proxy_handle room)
184 GtkFileFilter * filter;
185 GtkWidget * dialog;
186 char * filename;
188 dialog = gtk_file_chooser_dialog_new(
189 _("Load project"),
190 NULL,
191 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
192 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
193 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
194 NULL);
196 gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(dialog), FALSE);
198 #if 0
199 filter = gtk_file_filter_new();
200 gtk_file_filter_add_custom(filter, GTK_FILE_FILTER_FILENAME, reject_filter, dialog, NULL); /* reject all files */
201 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
202 #endif
203 //g_signal_connect(G_OBJECT(dialog), "selection-changed", G_CALLBACK(on_dir_select), dialog);
204 g_signal_connect(G_OBJECT(dialog), "current-folder-changed", G_CALLBACK(dir_changed), dialog);
206 loop:
207 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
209 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
211 if (!is_project_dir(filename))
213 GtkWidget * dialog;
214 dialog = get_gtk_builder_widget("error_dialog");
215 gtk_message_dialog_set_markup(GTK_MESSAGE_DIALOG(dialog), _("<b><big>Not a project dir</big></b>"));
216 gtk_message_dialog_format_secondary_markup(GTK_MESSAGE_DIALOG(dialog), "%s", filename);
217 gtk_widget_show(dialog);
218 gtk_dialog_run(GTK_DIALOG(dialog));
219 gtk_widget_hide(dialog);
220 goto loop;
223 //gtk_entry_set_text(GTK_ENTRY(get_gtk_builder_widget("load_project_path_entry")), filename);
224 log_info("Loading project from '%s'", filename);
225 if (!ladish_room_proxy_load_project(room, filename))
227 log_error("ladish_room_proxy_load_project() failed.");
230 g_free(filename);
232 //gtk_widget_activate(gtk_dialog_get_widget_for_response(GTK_DIALOG(data), GTK_RESPONSE_OK));
234 gtk_widget_destroy(dialog);
235 return;
238 #if 0
239 void ladish_run_load_project_dialog(ladish_room_proxy_handle room)
241 GtkWidget * dialog;
242 GtkWidget * browse_button;
243 GtkTreeView * view;
244 GtkResponseType response;
245 GtkListStore * store;
246 GtkCellRenderer * renderer;
247 GtkTreeSortable * sortable;
248 gint colno;
249 GtkTreeViewColumn * column;
250 GtkEntry * path;
252 dialog = get_gtk_builder_widget("load_project_dialog");
253 browse_button = get_gtk_builder_widget("load_project_path_browse_button");
254 path = GTK_ENTRY(get_gtk_builder_widget("load_project_path_entry"));
255 view = GTK_TREE_VIEW(get_gtk_builder_widget("loadable_project_list"));
257 store = GTK_LIST_STORE(gtk_tree_view_get_model(view));
258 if (store == NULL)
260 store = gtk_list_store_new(NUM_COLS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT64);
261 sortable = GTK_TREE_SORTABLE(store);
262 gtk_tree_view_set_model(view, GTK_TREE_MODEL(store));
264 renderer = gtk_cell_renderer_text_new();
266 colno = gtk_tree_view_insert_column_with_attributes(view, -1, "Project Name", renderer, NULL) - 1;
267 column = gtk_tree_view_get_column(view, colno);
268 gtk_tree_view_column_set_sort_column_id(column, COL_NAME);
270 colno = gtk_tree_view_insert_column_with_attributes(view, -1, "Modified", renderer, NULL) - 1;
271 column = gtk_tree_view_get_column(view, colno);
272 gtk_tree_view_column_set_sort_column_id(column, COL_MODIFIED);
273 gtk_tree_sortable_set_sort_func(sortable, colno, mtime_sorter, store, NULL);
274 gtk_tree_sortable_set_sort_column_id(sortable, colno, GTK_SORT_ASCENDING);
276 colno = gtk_tree_view_insert_column_with_attributes(view, -1, "Description", renderer, NULL) - 1;
277 column = gtk_tree_view_get_column(view, colno);
278 gtk_tree_view_column_set_sort_column_id(column, COL_DESCRIPTION);
281 //log_info("store = %p", store);
283 /* Gtk::TreeModel::Row row; */
284 /* int result; */
286 /* for (std::list<lash_project_info>::iterator iter = projects.begin(); iter != projects.end(); iter++) */
287 /* { */
288 /* std::string str; */
289 /* row = *(_model->append()); */
290 /* row[_columns.name] = iter->name; */
291 /* convert_timestamp_to_string(iter->modification_time, str); */
292 /* row[_columns.modified] = str; */
293 /* row[_columns.description] = iter->description; */
294 /* row[_columns.mtime] = iter->modification_time; */
295 /* } */
297 /* _widget->signal_button_press_event().connect(sigc::mem_fun(*this, &LoadProjectDialog::on_button_press_event), false); */
298 /* _widget->signal_key_press_event().connect(sigc::mem_fun(*this, &LoadProjectDialog::on_key_press_event), false); */
300 g_signal_connect(G_OBJECT(browse_button), "clicked", G_CALLBACK(on_browse), dialog);
302 gtk_entry_set_text(path, "");
304 gtk_widget_show(dialog);
305 response = gtk_dialog_run(GTK_DIALOG(dialog));
306 gtk_widget_hide(dialog);
307 if (response == GTK_RESPONSE_OK)
309 log_info("Loading project from '%s'", gtk_entry_get_text(path));
310 if (!ladish_room_proxy_load_project(room, gtk_entry_get_text(path)))
312 log_error("ladish_room_proxy_load_project() failed.");
316 #endif