File: load_project_dialog.c

package info (click to toggle)
ladish 1%2Bdfsg0-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,940 kB
  • sloc: ansic: 36,406; python: 11,237; cpp: 705; makefile: 22; ruby: 20; sh: 17
file content (312 lines) | stat: -rw-r--r-- 8,959 bytes parent folder | download | duplicates (3)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*
 * LADI Session Handler (ladish)
 *
 * Copyright (C) 2010 Nedko Arnaudov <nedko@arnaudov.name>
 *
 **************************************************************************
 * This file contains implementation of the load project dialog
 **************************************************************************
 *
 * LADI Session Handler 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * LADI Session Handler 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 LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
 * or write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <sys/stat.h>

#include "load_project_dialog.h"
#include "gtk_builder.h"
#include "../common/catdup.h"

enum
{
  COL_NAME = 0,
  COL_MODIFIED,
  COL_DESCRIPTION,
  COL_MTIME,
  NUM_COLS
};

#if 0
static
void
convert_timestamp_to_string(
  const time_t timestamp,
  std::string& timestamp_string)
{
  GDate mtime, now;
  gint days_diff;
  struct tm tm_mtime;
  time_t time_now;
  const gchar *format;
  gchar buf[256];

  if (timestamp == 0)
  {
    timestamp_string = "Unknown";
    return;
  }

  localtime_r(&timestamp, &tm_mtime);

  g_date_set_time_t(&mtime, timestamp);
  time_now = time(NULL);
  g_date_set_time_t(&now, time_now);

  days_diff = g_date_get_julian(&now) - g_date_get_julian(&mtime);

  if (days_diff == 0)
  {
    format = "Today at %H:%M";
  }
  else if (days_diff == 1)
  {
    format = "Yesterday at %H:%M";
  }
  else
  {
    if (days_diff > 1 && days_diff < 7)
    {
      format = "%A"; /* Days from last week */
    }
    else
    {
      format = "%x"; /* Any other date */
    }
  }

  if (strftime(buf, sizeof(buf), format, &tm_mtime) != 0)
  {
    timestamp_string = buf;
  }
  else
  {
    timestamp_string = "Unknown";
  }
}
#endif

#if 0
static int mtime_sorter(GtkTreeModel * model, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data)
{
  uint64_t ta;
  uint64_t tb;

  gtk_tree_model_get(GTK_TREE_MODEL(user_data), a, COL_MTIME, &ta, -1);
  gtk_tree_model_get(GTK_TREE_MODEL(user_data), b, COL_MTIME, &tb, -1);

  return ta > tb ? -1 : (ta == tb ? 0 : 1);
}
#endif

static gboolean reject_filter(const GtkFileFilterInfo * filter_info, gpointer data)
{
  //log_info("filter: '%s'", filter_info->filename);
  return FALSE;
}

static bool is_project_dir(const char * dir)
{
  char * file;
  struct stat st;
  int ret;

  file = catdup(dir, "/ladish-project.xml");
  if (file == NULL)
  {
    log_error("catdup() failed to compose project file path");
    return false;
  }

  ret = stat(file, &st);
  free(file);

  return ret == 0;
}

#if 0
static void on_dir_select(GtkWidget * widget, gpointer data)
{
  char * dir;
  gboolean sensitive;

  //log_info("selection-changed signal");

  dir = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(data));
  sensitive = FALSE;
  if (dir != NULL)
  {
    sensitive = is_project_dir(dir);
    g_free(dir);
  }

  gtk_widget_set_sensitive(gtk_dialog_get_widget_for_response(GTK_DIALOG(data), GTK_RESPONSE_ACCEPT), sensitive);
}
#endif

static void dir_changed(GtkWidget * widget, gpointer data)
{
  char * dir;

  //log_info("current-folder-changed signal");

  dir = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(data));
  if (dir == NULL)
  {
    return;
  }

  //log_info("dir changed: '%s'", dir);

  if (is_project_dir(dir))
  {
    gtk_widget_activate(gtk_dialog_get_widget_for_response(GTK_DIALOG(data), GTK_RESPONSE_ACCEPT));
  }
}

//static void on_browse(GtkWidget * widget, gpointer data)
void ladish_run_load_project_dialog(ladish_room_proxy_handle room)
{
  GtkFileFilter * filter;
  GtkWidget * dialog;
  char * filename;

  dialog = gtk_file_chooser_dialog_new(
    _("Load project"),
    NULL,
    GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
    GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
    GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
    NULL);

  gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(dialog), FALSE);

  filter = gtk_file_filter_new();
  gtk_file_filter_add_custom(filter, GTK_FILE_FILTER_FILENAME, reject_filter, dialog, NULL); /* reject all files */
  gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
  //g_signal_connect(G_OBJECT(dialog), "selection-changed", G_CALLBACK(on_dir_select), dialog);
  g_signal_connect(G_OBJECT(dialog), "current-folder-changed", G_CALLBACK(dir_changed), dialog);

loop:
  if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
  {
    filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));

    if (!is_project_dir(filename))
    {
      GtkWidget * dialog;
      dialog = get_gtk_builder_widget("error_dialog");
      gtk_message_dialog_set_markup(GTK_MESSAGE_DIALOG(dialog), _("<b><big>Not a project dir</big></b>"));
      gtk_message_dialog_format_secondary_markup(GTK_MESSAGE_DIALOG(dialog), "%s", filename);
      gtk_widget_show(dialog);
      gtk_dialog_run(GTK_DIALOG(dialog));
      gtk_widget_hide(dialog);
      goto loop;
    }

    //gtk_entry_set_text(GTK_ENTRY(get_gtk_builder_widget("load_project_path_entry")), filename);
    log_info("Loading project from '%s'", filename);
    if (!ladish_room_proxy_load_project(room, filename))
    {
      log_error("ladish_room_proxy_load_project() failed.");
    }

    g_free(filename);

    //gtk_widget_activate(gtk_dialog_get_widget_for_response(GTK_DIALOG(data), GTK_RESPONSE_OK));
  }
  gtk_widget_destroy(dialog);
  return;
}

#if 0
void ladish_run_load_project_dialog(ladish_room_proxy_handle room)
{
  GtkWidget * dialog;
  GtkWidget * browse_button;
  GtkTreeView * view;
  GtkResponseType response;
  GtkListStore * store;
  GtkCellRenderer * renderer;
  GtkTreeSortable * sortable;
  gint colno;
  GtkTreeViewColumn * column;
  GtkEntry * path;

  dialog = get_gtk_builder_widget("load_project_dialog");
  browse_button = get_gtk_builder_widget("load_project_path_browse_button");
  path = GTK_ENTRY(get_gtk_builder_widget("load_project_path_entry"));
  view = GTK_TREE_VIEW(get_gtk_builder_widget("loadable_project_list"));

  store = GTK_LIST_STORE(gtk_tree_view_get_model(view));
  if (store == NULL)
  {
    store = gtk_list_store_new(NUM_COLS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_UINT64);
    sortable = GTK_TREE_SORTABLE(store);
    gtk_tree_view_set_model(view, GTK_TREE_MODEL(store));

    renderer = gtk_cell_renderer_text_new();

    colno = gtk_tree_view_insert_column_with_attributes(view, -1, "Project Name", renderer, NULL) - 1;
    column = gtk_tree_view_get_column(view, colno);
    gtk_tree_view_column_set_sort_column_id(column, COL_NAME);

    colno = gtk_tree_view_insert_column_with_attributes(view, -1, "Modified", renderer, NULL) - 1;
    column = gtk_tree_view_get_column(view, colno);
    gtk_tree_view_column_set_sort_column_id(column, COL_MODIFIED);
    gtk_tree_sortable_set_sort_func(sortable, colno, mtime_sorter, store, NULL);
    gtk_tree_sortable_set_sort_column_id(sortable, colno, GTK_SORT_ASCENDING);

    colno = gtk_tree_view_insert_column_with_attributes(view, -1, "Description", renderer, NULL) - 1;
    column = gtk_tree_view_get_column(view, colno);
    gtk_tree_view_column_set_sort_column_id(column, COL_DESCRIPTION);
  }

  //log_info("store = %p", store);

  /* Gtk::TreeModel::Row row; */
  /* int result; */

  /* for (std::list<lash_project_info>::iterator iter = projects.begin(); iter != projects.end(); iter++) */
  /* { */
  /*   std::string str; */
  /*   row = *(_model->append()); */
  /*   row[_columns.name] = iter->name; */
  /*   convert_timestamp_to_string(iter->modification_time, str); */
  /*   row[_columns.modified] = str; */
  /*   row[_columns.description] = iter->description; */
  /*   row[_columns.mtime] = iter->modification_time; */
  /* } */

  /* _widget->signal_button_press_event().connect(sigc::mem_fun(*this, &LoadProjectDialog::on_button_press_event), false); */
  /* _widget->signal_key_press_event().connect(sigc::mem_fun(*this, &LoadProjectDialog::on_key_press_event), false); */

  g_signal_connect(G_OBJECT(browse_button), "clicked", G_CALLBACK(on_browse), dialog);

  gtk_entry_set_text(path, "");

  gtk_widget_show(dialog);
  response = gtk_dialog_run(GTK_DIALOG(dialog));
  gtk_widget_hide(dialog);
  if (response == GTK_RESPONSE_OK)
  {
    log_info("Loading project from '%s'", gtk_entry_get_text(path));
    if (!ladish_room_proxy_load_project(room, gtk_entry_get_text(path)))
    {
      log_error("ladish_room_proxy_load_project() failed.");
    }
  }
}
#endif