Warnings: missing initializer for value_tables and function type cast (GObject)
[pspp] / src / ui / gui / psppire-text-file.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2017, 2020 Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18 #include <gettext.h>
19 #define _(msgid) gettext (msgid)
20 #define P_(msgid) msgid
21
22 #include "psppire-text-file.h"
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <sys/stat.h>
26 #include <stdlib.h>
27 #include "libpspp/line-reader.h"
28 #include "libpspp/message.h"
29 #include "libpspp/str.h"
30 #include "libpspp/i18n.h"
31
32 #include <gtk/gtk.h>
33
34 /* Properties */
35 enum
36   {
37     PROP_0,
38     PROP_FILE_NAME,
39     PROP_ENCODING,
40     PROP_MAXIMUM_LINES,
41     PROP_LINE_COUNT
42   };
43
44 enum {MAX_LINE_LEN = 16384};  /* Max length of an acceptable line. */
45
46 static void
47 read_lines (PsppireTextFile *tf)
48 {
49   if (tf->file_name && 0 != g_strcmp0 ("unset", tf->encoding))
50     {
51       struct line_reader *reader = line_reader_for_file (tf->encoding, tf->file_name, O_RDONLY);
52
53       if (reader == NULL)
54         {
55           msg_error (errno, _("Could not open `%s'"),  tf->file_name);
56           return;
57         }
58
59       struct string input;
60       ds_init_empty (&input);
61       for (tf->line_cnt = 0; tf->line_cnt < MAX_PREVIEW_LINES; tf->line_cnt++)
62         {
63           ds_clear (&input);
64           if (!line_reader_read (reader, &input, MAX_LINE_LEN + 1)
65               || ds_length (&input) > MAX_LINE_LEN)
66             {
67               int i;
68               if (line_reader_eof (reader))
69                 break;
70               else if (line_reader_error (reader))
71                 msg (ME, _("Error reading `%s': %s"),
72                      tf->file_name, strerror (line_reader_error (reader)));
73               else
74                 msg (ME, _("Failed to read `%s', because it contains a line "
75                            "over %d bytes long and therefore appears not to be "
76                            "a text file."),
77                      tf->file_name, MAX_LINE_LEN);
78               line_reader_close (reader);
79               for (i = 0; i < tf->line_cnt; i++)
80                 g_free (tf->lines[i].string);
81               tf->line_cnt = 0;
82               ds_destroy (&input);
83               return;
84             }
85
86           tf->lines[tf->line_cnt]
87             = recode_substring_pool ("UTF-8",
88                                      line_reader_get_encoding (reader),
89                                      input.ss, NULL);
90         }
91       ds_destroy (&input);
92
93       if (tf->line_cnt == 0)
94         {
95           int i;
96           msg (ME, _("`%s' is empty."), tf->file_name);
97           line_reader_close (reader);
98           for (i = 0; i < tf->line_cnt; i++)
99             g_free (tf->lines[i].string);
100           tf->line_cnt = 0;
101           goto done;
102         }
103
104       if (tf->line_cnt < MAX_PREVIEW_LINES)
105         {
106           tf->total_lines = tf->line_cnt;
107           tf->total_is_exact = true;
108         }
109       else
110         {
111           /* Estimate the number of lines in the file. */
112           struct stat s;
113           off_t position = line_reader_tell (reader);
114           if (fstat (line_reader_fileno (reader), &s) == 0 && position > 0)
115             {
116               tf->total_lines = (double) tf->line_cnt / position * s.st_size;
117               tf->total_is_exact = false;
118             }
119           else
120             {
121               tf->total_lines = 0;
122               tf->total_is_exact = true;
123             }
124         }
125     done:
126       line_reader_close (reader);
127     }
128 }
129
130 static void
131 psppire_text_file_set_property (GObject         *object,
132                                 guint            prop_id,
133                                 const GValue    *value,
134                                 GParamSpec      *pspec)
135 {
136   PsppireTextFile *tf = PSPPIRE_TEXT_FILE (object);
137
138   switch (prop_id)
139     {
140     case PROP_MAXIMUM_LINES:
141       tf->maximum_lines = g_value_get_int (value);
142       break;
143     case PROP_FILE_NAME:
144       tf->file_name = g_value_dup_string (value);
145       read_lines (tf);
146       break;
147     case PROP_ENCODING:
148       g_free (tf->encoding);
149       tf->encoding = g_value_dup_string (value);
150       read_lines (tf);
151       break;
152     default:
153       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
154       break;
155     };
156
157 }
158
159 static void
160 psppire_text_file_get_property (GObject         *object,
161                                 guint            prop_id,
162                                 GValue          *value,
163                                 GParamSpec      *pspec)
164 {
165   PsppireTextFile *text_file = PSPPIRE_TEXT_FILE (object);
166
167   switch (prop_id)
168     {
169     case PROP_MAXIMUM_LINES:
170       g_value_set_int (value, text_file->maximum_lines);
171       break;
172     case PROP_LINE_COUNT:
173       g_value_set_int (value, text_file->line_cnt);
174       break;
175     case PROP_FILE_NAME:
176       g_value_set_string (value, text_file->file_name);
177       break;
178     case PROP_ENCODING:
179       g_value_set_string (value, text_file->encoding);
180       break;
181     default:
182       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
183       break;
184     };
185 }
186
187 static void psppire_text_file_finalize        (GObject           *object);
188 static void psppire_text_file_dispose        (GObject           *object);
189
190 static GObjectClass *parent_class = NULL;
191
192 static gboolean
193 __tree_get_iter (GtkTreeModel *tree_model,
194                  GtkTreeIter *iter,
195                  GtkTreePath *path)
196 {
197   PsppireTextFile *file  = PSPPIRE_TEXT_FILE (tree_model);
198
199   if (path == NULL)
200     return FALSE;
201
202   gint *indices = gtk_tree_path_get_indices (path);
203
204   gint n = *indices;
205
206   if (n >= file->line_cnt)
207     return FALSE;
208
209   iter->user_data = GINT_TO_POINTER (n);
210   iter->stamp = file->stamp;
211
212   return TRUE;
213 }
214
215
216 static gboolean
217 __tree_iter_next (GtkTreeModel *tree_model,
218                   GtkTreeIter *iter)
219 {
220   PsppireTextFile *file  = PSPPIRE_TEXT_FILE (tree_model);
221   g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
222
223   gint n = GPOINTER_TO_INT (iter->user_data) + 1;
224
225   if (n >= file->line_cnt)
226     return FALSE;
227
228   iter->user_data = GINT_TO_POINTER (n);
229
230   return TRUE;
231 }
232
233
234 static GType
235 __tree_get_column_type (GtkTreeModel *tree_model,
236                         gint          index)
237 {
238   if (index == 0)
239     return G_TYPE_INT;
240
241   return G_TYPE_STRING;
242 }
243
244 static gboolean
245 __iter_has_child (GtkTreeModel *tree_model,
246                   GtkTreeIter  *iter)
247 {
248   return 0;
249 }
250
251
252 static gboolean
253 __iter_parent     (GtkTreeModel *tree_model,
254                    GtkTreeIter  *iter,
255                    GtkTreeIter  *child)
256 {
257   return 0;
258 }
259
260 static GtkTreePath *
261 __tree_get_path (GtkTreeModel *tree_model,
262                  GtkTreeIter  *iter)
263 {
264   PsppireTextFile *file  = PSPPIRE_TEXT_FILE (tree_model);
265   g_return_val_if_fail (file->stamp == iter->stamp, FALSE);
266
267   gint n = GPOINTER_TO_INT (iter->user_data);
268
269   return gtk_tree_path_new_from_indices (n, -1);
270 }
271
272
273 static gboolean
274 __iter_children (GtkTreeModel *tree_model,
275                               GtkTreeIter *iter,
276                               GtkTreeIter *parent)
277 {
278   return 0;
279 }
280
281
282 static gint
283 __tree_model_iter_n_children (GtkTreeModel *tree_model,
284                               GtkTreeIter *iter)
285 {
286   PsppireTextFile *file  = PSPPIRE_TEXT_FILE (tree_model);
287   g_assert (iter == NULL);
288   return file->line_cnt;
289 }
290
291 static GtkTreeModelFlags
292 __tree_model_get_flags (GtkTreeModel *model)
293 {
294   g_return_val_if_fail (PSPPIRE_IS_TEXT_FILE (model), (GtkTreeModelFlags) 0);
295
296   return GTK_TREE_MODEL_LIST_ONLY;
297 }
298
299 static gint
300 __tree_model_get_n_columns (GtkTreeModel *tree_model)
301 {
302   return 2;
303 }
304
305
306 static gboolean
307 __iter_nth_child (GtkTreeModel *tree_model,
308                   GtkTreeIter *iter,
309                   GtkTreeIter *parent,
310                   gint n)
311 {
312   PsppireTextFile *file  = PSPPIRE_TEXT_FILE (tree_model);
313
314   g_assert (parent == NULL);
315
316   g_return_val_if_fail (file, FALSE);
317
318   if (n >= file->line_cnt)
319     {
320       iter->stamp = -1;
321       iter->user_data = NULL;
322       return FALSE;
323     }
324
325   iter->user_data = GINT_TO_POINTER (n);
326   iter->stamp = file->stamp;
327
328   return TRUE;
329 }
330
331
332 static void
333 __get_value (GtkTreeModel *tree_model,
334              GtkTreeIter *iter,
335              gint column,
336              GValue *value)
337 {
338   PsppireTextFile *file  = PSPPIRE_TEXT_FILE (tree_model);
339
340   g_return_if_fail (iter->stamp == file->stamp);
341
342   gint n = GPOINTER_TO_INT (iter->user_data);
343
344   g_return_if_fail (n < file->line_cnt);
345
346   if (column == 0)
347     {
348       g_value_init (value, G_TYPE_INT);
349       g_value_set_int (value, n + 1);
350       return;
351     }
352
353   g_value_init (value, G_TYPE_STRING);
354
355   if (column == 1)
356     {
357       char *s = ss_xstrdup (file->lines[n]);
358       g_value_set_string (value, s);
359       free (s);
360       return;
361     }
362
363   g_assert_not_reached ();
364 }
365
366
367 static void
368 __tree_model_init (GtkTreeModelIface *iface)
369 {
370   iface->get_flags       = __tree_model_get_flags;
371   iface->get_n_columns   = __tree_model_get_n_columns ;
372   iface->get_column_type = __tree_get_column_type;
373   iface->get_iter        = __tree_get_iter;
374   iface->iter_next       = __tree_iter_next;
375   iface->get_path        = __tree_get_path;
376   iface->get_value       = __get_value;
377
378   iface->iter_children   = __iter_children;
379   iface->iter_has_child  = __iter_has_child;
380   iface->iter_n_children = __tree_model_iter_n_children;
381   iface->iter_nth_child  = __iter_nth_child;
382   iface->iter_parent     = __iter_parent;
383 }
384
385 G_DEFINE_TYPE_WITH_CODE (PsppireTextFile, psppire_text_file, G_TYPE_OBJECT,
386                          G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
387                                                 __tree_model_init))
388
389 static void
390 psppire_text_file_class_init (PsppireTextFileClass *class)
391 {
392   GObjectClass *object_class;
393
394   parent_class = g_type_class_peek_parent (class);
395   object_class = (GObjectClass*) class;
396
397   GParamSpec *maximum_lines_spec =
398     g_param_spec_int ("maximum-lines",
399                       "Maximum Lines",
400                       P_("An upper limit on the number of lines to consider"),
401                       0, G_MAXINT, G_MAXINT,
402                       G_PARAM_READWRITE);
403
404   GParamSpec *line_count_spec =
405     g_param_spec_int ("line-count",
406                       "Line Count",
407                       P_("The number of lines in the file"),
408                       0, G_MAXINT, G_MAXINT,
409                       G_PARAM_READABLE);
410
411   GParamSpec *file_name_spec =
412     g_param_spec_string ("file-name",
413                          "File Name",
414                          P_("The name of the file from which this object was constructed"),
415                          NULL,
416                          G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
417
418   GParamSpec *encoding_spec =
419     g_param_spec_string ("encoding",
420                          "Character Encoding",
421                          P_("The character encoding of the file from which this object was constructed"),
422                          "unset",
423                          G_PARAM_CONSTRUCT_ONLY |G_PARAM_READWRITE);
424
425   object_class->set_property = psppire_text_file_set_property;
426   object_class->get_property = psppire_text_file_get_property;
427
428   g_object_class_install_property (object_class,
429                                    PROP_MAXIMUM_LINES,
430                                    maximum_lines_spec);
431
432   g_object_class_install_property (object_class,
433                                    PROP_LINE_COUNT,
434                                    line_count_spec);
435
436   g_object_class_install_property (object_class,
437                                    PROP_FILE_NAME,
438                                    file_name_spec);
439
440   g_object_class_install_property (object_class,
441                                    PROP_ENCODING,
442                                    encoding_spec);
443
444   object_class->finalize = psppire_text_file_finalize;
445   object_class->dispose = psppire_text_file_dispose;
446 }
447
448 static void
449 psppire_text_file_init (PsppireTextFile *text_file)
450 {
451   text_file->encoding = g_strdup ("unset");
452   text_file->file_name = NULL;
453
454   text_file->dispose_has_run = FALSE;
455   text_file->stamp = g_random_int ();
456 }
457
458
459 PsppireTextFile *
460 psppire_text_file_new (const gchar *file_name, const gchar *encoding)
461 {
462   PsppireTextFile *retval =
463     g_object_new (PSPPIRE_TYPE_TEXT_FILE,
464                   "file-name", file_name,
465                   "encoding", encoding,
466                   NULL);
467
468   return retval;
469 }
470
471 static void
472 psppire_text_file_finalize (GObject *object)
473 {
474   PsppireTextFile *tf = PSPPIRE_TEXT_FILE (object);
475
476   for (int i = 0; i < tf->line_cnt; i++)
477     g_free (tf->lines[i].string);
478
479   g_free (tf->encoding);
480   g_free (tf->file_name);
481
482   /* must chain up */
483   (* parent_class->finalize) (object);
484 }
485
486
487 static void
488 psppire_text_file_dispose (GObject *object)
489 {
490   PsppireTextFile *ds = PSPPIRE_TEXT_FILE (object);
491
492   if (ds->dispose_has_run)
493     return;
494
495   /* must chain up */
496   (* parent_class->dispose) (object);
497
498   ds->dispose_has_run = TRUE;
499 }
500
501 gboolean
502 psppire_text_file_get_total_exact (PsppireTextFile *tf)
503 {
504   return tf->total_is_exact;
505 }
506
507 gulong
508 psppire_text_file_get_n_lines (PsppireTextFile *tf)
509 {
510   return tf->total_lines;
511 }