oupis-window.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* oupis-window.c
  2. *
  3. * Copyright 2026 Thomas Arnoux
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. * SPDX-License-Identifier: GPL-3.0-or-later
  19. */
  20. #include "config.h"
  21. #include "oupis-window.h"
  22. struct _OupisWindow
  23. {
  24. AdwApplicationWindow parent_instance;
  25. /* Template widgets */
  26. GtkSearchEntry *searchentry;
  27. GtkScrolledWindow *scrolledwindow;
  28. GtkListView *listview;
  29. GtkSingleSelection *singleselection;
  30. GListStore *liststore;
  31. };
  32. G_DEFINE_FINAL_TYPE (OupisWindow, oupis_window, ADW_TYPE_APPLICATION_WINDOW)
  33. static gchar*
  34. str_to_lc_ascii (const gchar* string)
  35. {
  36. gchar* str_lc_ascii;
  37. str_lc_ascii = g_utf8_strdown (string, -1);
  38. return g_str_to_ascii (str_lc_ascii, NULL);
  39. }
  40. static void
  41. chercher_una (GMatchInfo *match_info,
  42. OupisWindow *window)
  43. {
  44. const gchar* procdir = "/home/vetetix/Code/Gendarmerie/Oupis/procedures";
  45. //gchar proctype[][6];
  46. GDateTime *now;
  47. gint year;
  48. int unite;
  49. int numero;
  50. int annee;
  51. // Il vaudrait mieux utiliser la fonction g_ascii_strtoll puis transformer le
  52. // gint64 en int (https://stackoverflow.com/questions/3298612/uint64-t-to-int)
  53. // mais atoi devrait être suffisamment sûr puisque les variables ont été
  54. // validées par la regex.
  55. unite = atoi (g_match_info_fetch_named (match_info, "unite"));
  56. numero = atoi (g_match_info_fetch_named (match_info, "numero"));
  57. annee = atoi (g_match_info_fetch_named (match_info, "annee"));
  58. g_print ("unite: %.5i\n", unite);
  59. g_print ("numero: %.5i\n", numero);
  60. g_print ("annee: %.4i\n", annee);
  61. g_autoptr(GStrvBuilder) builder = g_strv_builder_new ();
  62. g_strv_builder_add_many (builder,
  63. "Administratif",
  64. "Judiciaire",
  65. "Courriers",
  66. "MainCourante",
  67. "Militaire",
  68. NULL);
  69. g_auto(GStrv) proctype = g_strv_builder_end (builder);
  70. g_autoptr (GFile) category_dir;
  71. g_autoptr (GFileEnumerator) direnum;
  72. g_autoptr (GFileInfo) info;
  73. g_autoptr (GFile) file;
  74. GError *error = NULL;
  75. gchar* infoname;
  76. gchar* infoname_lc_ascii;
  77. int i=0;
  78. for (i=0; i < g_strv_length (proctype); i++ ) {
  79. category_dir = g_file_new_build_filename (procdir,
  80. proctype[i],
  81. NULL);
  82. g_print ("%s\n", g_file_get_path (category_dir));
  83. //TODO rendre async et gérer les erreurs
  84. direnum = g_file_enumerate_children (category_dir,
  85. "standard::name,standard::display-name,standard::type",
  86. G_FILE_QUERY_INFO_NONE,
  87. NULL,
  88. &error);
  89. if (error) {
  90. g_print("%s\n", error->message);
  91. error = NULL;
  92. return;
  93. }
  94. while (TRUE)
  95. {
  96. if (!g_file_enumerator_iterate (direnum, &info, &file, NULL, &error))
  97. goto out;
  98. if (!info)
  99. break;
  100. g_print ("%s\n", g_file_get_path (file));
  101. infoname = g_file_info_get_name (info);
  102. /*infoname_lc_ascii = str_to_lc_ascii (infoname);
  103. if (g_strrstr (infoname_lc_ascii, request_lc_ascii)) {
  104. g_print ("%s\n", infoname);
  105. g_list_store_insert (window->liststore, 0, file);
  106. }*/
  107. }
  108. out:
  109. if (error) {
  110. g_print("%s\n", error->message);
  111. error = NULL;
  112. return;
  113. }
  114. //g_object_unref (direnum); // Note: frees the last @info
  115. }
  116. g_error_free (error);
  117. now = g_date_time_new_now_local ();
  118. year = g_date_time_get_year (now);
  119. g_print ("%i\n",year);
  120. }
  121. static void
  122. oupis_window__search_request (GtkSearchEntry *self,
  123. OupisWindow *window)
  124. {
  125. const char* request;
  126. char* request_lc_ascii;
  127. GFile *directory;
  128. GFileEnumerator *direnum;
  129. GError *error = NULL;
  130. GFileInfo *info;
  131. GFile *file;
  132. const gchar * infoname;
  133. gchar * infoname_lc_ascii;
  134. g_list_store_remove_all (window->liststore);
  135. request = gtk_editable_get_text (GTK_EDITABLE (self));
  136. g_print ("Request: %s\n", request);
  137. request_lc_ascii = str_to_lc_ascii (request);
  138. g_print (" R_l_a: %s\n", request_lc_ascii);
  139. // On détermine le type de requête :
  140. // - commande (/commande arguments)
  141. // - una (UUUUU/NNNNN/AAAA)
  142. // - texte (tout le reste)
  143. // Gestion des commandes
  144. if (g_pattern_match_simple ("/*", request_lc_ascii))
  145. {
  146. g_print ("C'est une commande\n");
  147. return;
  148. }
  149. // Gestion des recherches d'UNA
  150. const char *regex_pattern = "^(?:(?P<unite>[0-9]{0,5})(?=/[0-9]{1,5}/[0-9]{1,4})/)?(?P<numero>[0-9]{1,5})(?:/(?P<annee>[0-9]{1,4}))?$";
  151. g_autoptr(GMatchInfo) match_info = NULL;
  152. g_autoptr(GRegex) regex = NULL;
  153. regex = g_regex_new (regex_pattern, G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, NULL);
  154. g_assert (regex != NULL);
  155. if (g_regex_match (regex, request_lc_ascii, G_REGEX_MATCH_DEFAULT, &match_info))
  156. {
  157. int start_pos, end_pos;
  158. g_match_info_fetch_pos (match_info, 0, &start_pos, &end_pos);
  159. g_print ("Match successful! Overall pattern matches bytes %d to %d\n", start_pos, end_pos);
  160. g_print ("Count: %i\n", g_match_info_get_match_count(match_info));
  161. g_print ("0. %s\n", g_match_info_fetch (match_info, 0));
  162. g_print ("1.U%s\n", g_match_info_fetch (match_info, 1));
  163. g_print ("2.N%s\n", g_match_info_fetch (match_info, 2));
  164. g_print ("3.A%s\n", g_match_info_fetch (match_info, 3));
  165. g_print ("4. %s\n", g_match_info_fetch (match_info, 4));
  166. g_print ("5. %s\n", g_match_info_fetch (match_info, 5));
  167. g_print ("6. %s\n", g_match_info_fetch (match_info, 6));
  168. g_print ("7. %s\n", g_match_info_fetch (match_info, 7));
  169. chercher_una (match_info, window);
  170. return;
  171. }
  172. else
  173. {
  174. g_print ("No match!\n");
  175. }
  176. directory = g_file_new_for_path (".");
  177. direnum = g_file_enumerate_children (directory,
  178. "standard::name,standard::display-name",
  179. G_FILE_QUERY_INFO_NONE,
  180. NULL,
  181. NULL);
  182. g_object_unref (directory);
  183. while (TRUE)
  184. {
  185. if (!g_file_enumerator_iterate (direnum, &info, &file, NULL, &error))
  186. goto out;
  187. if (!info)
  188. break;
  189. infoname = g_file_info_get_name (info);
  190. infoname_lc_ascii = str_to_lc_ascii (infoname);
  191. if (g_strrstr (infoname_lc_ascii, request_lc_ascii)) {
  192. g_print ("%s\n", infoname);
  193. g_list_store_insert (window->liststore, 0, file);
  194. }
  195. }
  196. out:
  197. if (error) {
  198. g_print("%s\n", error->message);
  199. g_error_free (error);
  200. }
  201. g_object_unref (direnum); // Note: frees the last @info
  202. g_free (request_lc_ascii);
  203. g_free (infoname_lc_ascii);
  204. }
  205. static void
  206. listview_activate (GtkListView *listview, int position, OupisWindow *window) {
  207. GError *error = NULL;
  208. GFile *file = G_FILE (g_list_model_get_item (G_LIST_MODEL (gtk_list_view_get_model (listview)), position));;
  209. //GFileInfo *info = G_FILE_INFO (g_list_model_get_item (G_LIST_MODEL (gtk_list_view_get_model (listview)), position));
  210. g_print ("%s\n", g_file_get_basename (file));
  211. //file = G_FILE (g_file_info_get_attribute_object (info, "standard::file"));
  212. //TODO rendre cet appel async et gérer les éventuelles erreurs
  213. g_app_info_launch_default_for_uri (g_file_get_uri (file), NULL, &error);
  214. if (error){
  215. g_print ("%s\n", error->message);
  216. }
  217. g_object_unref (file);
  218. }
  219. static GIcon *
  220. get_icon (GtkListItem *item, GFileInfo *info) {
  221. GIcon *icon;
  222. /* g_file_info_get_icon can return NULL */
  223. //TODO corriger ou supprimer
  224. icon = G_IS_FILE_INFO (info) ? g_file_info_get_icon (info) : NULL;
  225. return icon ? g_object_ref (icon) : NULL;
  226. }
  227. static char *
  228. get_file_name (GtkListItem *item, GFile *file) {
  229. return G_IS_FILE (file) ? g_strdup (g_file_get_basename (file)) : NULL;
  230. }
  231. static void
  232. oupis_window_class_init (OupisWindowClass *klass)
  233. {
  234. GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  235. gtk_widget_class_set_template_from_resource (widget_class, "/lu/arnoux/Oupis/oupis-window.ui");
  236. gtk_widget_class_bind_template_child (widget_class, OupisWindow, searchentry);
  237. gtk_widget_class_bind_template_child (widget_class, OupisWindow, scrolledwindow);
  238. gtk_widget_class_bind_template_child (widget_class, OupisWindow, listview);
  239. gtk_widget_class_bind_template_child (widget_class, OupisWindow, singleselection);
  240. gtk_widget_class_bind_template_child (widget_class, OupisWindow, liststore);
  241. gtk_widget_class_bind_template_callback (widget_class, oupis_window__search_request);
  242. gtk_widget_class_bind_template_callback (widget_class, listview_activate);
  243. gtk_widget_class_bind_template_callback (widget_class, get_icon);
  244. gtk_widget_class_bind_template_callback (widget_class, get_file_name);
  245. }
  246. static void
  247. oupis_window_init (OupisWindow *self)
  248. {
  249. GFile *file;
  250. gtk_widget_init_template (GTK_WIDGET (self) );
  251. gtk_widget_grab_focus (GTK_WIDGET (self->searchentry));
  252. file = g_file_new_for_path (".");
  253. //gtk_directory_list_set_file (GTK_DIRECTORY_LIST (self->directorylist), file);
  254. g_object_unref (file);
  255. }