1 /* -*- Mode: C ; c-basic-offset: 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.
29 #include "load_project_dialog.h"
30 #include "gtk_builder.h"
31 #include "../common/catdup.h"
45 convert_timestamp_to_string(
46 const time_t timestamp
,
47 std::string
& timestamp_string
)
58 timestamp_string
= "Unknown";
62 localtime_r(×tamp
, &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
);
72 format
= "Today at %H:%M";
74 else if (days_diff
== 1)
76 format
= "Yesterday at %H:%M";
80 if (days_diff
> 1 && days_diff
< 7)
82 format
= "%A"; /* Days from last week */
86 format
= "%x"; /* Any other date */
90 if (strftime(buf
, sizeof(buf
), format
, &tm_mtime
) != 0)
92 timestamp_string
= buf
;
96 timestamp_string
= "Unknown";
102 static int mtime_sorter(GtkTreeModel
* model
, GtkTreeIter
* a
, GtkTreeIter
* b
, gpointer user_data
)
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);
115 static gboolean
reject_filter(const GtkFileFilterInfo
* UNUSED(filter_info
), gpointer
UNUSED(data
))
117 //log_info("filter: '%s'", filter_info->filename);
122 static bool is_project_dir(const char * dir
)
128 file
= catdup(dir
, "/ladish-project.xml");
131 log_error("catdup() failed to compose project file path");
135 ret
= stat(file
, &st
);
142 static void on_dir_select(GtkWidget
* widget
, gpointer data
)
147 //log_info("selection-changed signal");
149 dir
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(data
));
153 sensitive
= is_project_dir(dir
);
157 gtk_widget_set_sensitive(gtk_dialog_get_widget_for_response(GTK_DIALOG(data
), GTK_RESPONSE_ACCEPT
), sensitive
);
161 static void dir_changed(GtkWidget
* UNUSED(widget
), gpointer data
)
165 //log_info("current-folder-changed signal");
167 dir
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(data
));
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
;
188 dialog
= gtk_file_chooser_dialog_new(
191 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
,
192 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
193 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
,
196 gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(dialog
), FALSE
);
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
);
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
);
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
))
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
);
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.");
232 //gtk_widget_activate(gtk_dialog_get_widget_for_response(GTK_DIALOG(data), GTK_RESPONSE_OK));
234 gtk_widget_destroy(dialog
);
239 void ladish_run_load_project_dialog(ladish_room_proxy_handle room
)
242 GtkWidget
* browse_button
;
244 GtkResponseType response
;
245 GtkListStore
* store
;
246 GtkCellRenderer
* renderer
;
247 GtkTreeSortable
* sortable
;
249 GtkTreeViewColumn
* column
;
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
));
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; */
286 /* for (std::list<lash_project_info>::iterator iter = projects.begin(); iter != projects.end(); iter++) */
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; */
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.");