1 /*
2  * Copyright (C) 2009 - 2011 Vivien Malerba <malerba@gnome-db.org>
3  * Copyright (C) 2011 Murray Cumming <murrayc@murrayc.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19 
20 #ifndef __QUERY_EDITOR_H__
21 #define __QUERY_EDITOR_H__
22 
23 #include <gtk/gtk.h>
24 #include <libgda/libgda.h>
25 
26 G_BEGIN_DECLS
27 
28 #define QUERY_TYPE_EDITOR            (query_editor_get_type())
29 #define QUERY_EDITOR(obj)            (G_TYPE_CHECK_INSTANCE_CAST (obj, QUERY_TYPE_EDITOR, QueryEditor))
30 #define QUERY_EDITOR_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST (klass, QUERY_TYPE_EDITOR, QueryEditorClass))
31 #define QUERY_IS_EDITOR(obj)         (G_TYPE_CHECK_INSTANCE_TYPE (obj, QUERY_TYPE_EDITOR))
32 #define QUERY_IS_EDITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), QUERY_TYPE_EDITOR))
33 
34 #define QUERY_EDITOR_TOOLTIP _("Enter SQL code to execute\n(must be understood by the database to\n" \
35 			       "which the connection is opened, except for the variables definition)\n" \
36 			       "The following shortcuts are allowed:\n" \
37 			       "   <small><b>CTRL - l</b></small> to clear the editor\n" \
38 			       "   <small><b>CTRL - ENTER</b></small> to execute SQL\n" \
39 			       "   <small><b>CTRL - Up</b></small> to move to previous executed SQL in history\n" \
40 			       "   <small><b>CTRL - Down</b></small> to move to next executed SQL in history\n" \
41 			       "   <small><b>CTRL - SPACE</b></small> to obtain a completion list")
42 
43 typedef struct _QueryEditor        QueryEditor;
44 typedef struct _QueryEditorClass   QueryEditorClass;
45 typedef struct _QueryEditorPrivate QueryEditorPrivate;
46 
47 /*
48  * Query history item
49  */
50 typedef struct {
51 	gchar *sql;
52 	GObject *result;
53 	GError *exec_error;
54 	gboolean within_transaction;
55 
56 	gint ref_count;
57 } QueryEditorHistoryItem;
58 
59 QueryEditorHistoryItem *query_editor_history_item_new (const gchar *sql, GObject *result, GError *error);
60 QueryEditorHistoryItem *query_editor_history_item_ref (QueryEditorHistoryItem *qih);
61 void                    query_editor_history_item_unref (QueryEditorHistoryItem *qih);
62 
63 /*
64  * Query history batch
65  */
66 typedef struct {
67 	GTimeVal run_time;
68 	GdaSet *params;
69 	GSList *hist_items; /* list of QueryEditorHistoryItem, ref held here */
70 
71 	gint ref_count;
72 } QueryEditorHistoryBatch;
73 
74 QueryEditorHistoryBatch *query_editor_history_batch_new (GTimeVal run_time, GdaSet *params);
75 QueryEditorHistoryBatch *query_editor_history_batch_ref (QueryEditorHistoryBatch *qib);
76 void                     query_editor_history_batch_unref (QueryEditorHistoryBatch *qib);
77 
78 
79 struct _QueryEditor {
80 	GtkBox parent;
81 	QueryEditorPrivate *priv;
82 };
83 
84 struct _QueryEditorClass {
85 	GtkBoxClass parent_class;
86 
87 	/* signals */
88 	void (* changed) (QueryEditor *editor);
89 	void (* history_item_removed) (QueryEditor *editor, QueryEditorHistoryItem *item);
90 	void (* history_cleared) (QueryEditor *editor);
91 	void (* execute_request) (QueryEditor *editor);
92 };
93 
94 /*
95  * Editor modes
96  */
97 typedef enum {
98 	QUERY_EDITOR_READWRITE,
99 	QUERY_EDITOR_READONLY,
100 	QUERY_EDITOR_HISTORY
101 } QueryEditorMode;
102 
103 
104 GType      query_editor_get_type (void) G_GNUC_CONST;
105 GtkWidget *query_editor_new (void);
106 
107 /* common API */
108 void       query_editor_set_mode (QueryEditor *editor, QueryEditorMode mode);
109 QueryEditorMode query_editor_get_mode (QueryEditor *editor);
110 
111 gchar     *query_editor_get_all_text (QueryEditor *editor);
112 gboolean   query_editor_load_from_file (QueryEditor *editor, const gchar *filename);
113 gboolean   query_editor_save_to_file (QueryEditor *editor, const gchar *filename);
114 
115 void       query_editor_copy_clipboard (QueryEditor *editor);
116 void       query_editor_cut_clipboard (QueryEditor *editor);
117 void       query_editor_paste_clipboard (QueryEditor *editor);
118 
119 /* normal editor's API */
120 void       query_editor_set_text (QueryEditor *editor, const gchar *text);
121 void       query_editor_append_text (QueryEditor *editor, const gchar *text);
122 void       query_editor_keep_current_state (QueryEditor *editor);
123 void       query_editor_append_note (QueryEditor *editor, const gchar *text, gint level);
124 void       query_editor_show_tooltip (QueryEditor *editor, gboolean show_tooltip);
125 
126 /* history API */
127 void       query_editor_start_history_batch (QueryEditor *editor, QueryEditorHistoryBatch *hist_batch);
128 void       query_editor_add_history_item (QueryEditor *editor, QueryEditorHistoryItem *hist_item);
129 QueryEditorHistoryItem *query_editor_get_current_history_item (QueryEditor *editor,
130 							       QueryEditorHistoryBatch **out_in_batch);
131 QueryEditorHistoryBatch *query_editor_get_current_history_batch (QueryEditor *editor);
132 gboolean   query_editor_history_is_empty (QueryEditor *editor);
133 
134 void       query_editor_del_all_history_items (QueryEditor *editor);
135 void       query_editor_del_current_history_item (QueryEditor *editor);
136 void       query_editor_del_history_batch (QueryEditor *editor, QueryEditorHistoryBatch *batch);
137 
138 G_END_DECLS
139 
140 #endif
141