Line data Source code
1 : /* File: gui_clipboard.c; Copyright and License: see below */
2 :
3 : #include "gui_clipboard.h"
4 : #include "gui_toolbox.h"
5 : #include "gui_tool.h"
6 : #include "u8/u8_trace.h"
7 : #include "u8/u8_error.h"
8 : #include "set/data_stat.h"
9 : #include "utf8stringbuf/utf8string.h"
10 : #include <assert.h>
11 : #include <gtk/gtk.h>
12 : #include <stdbool.h>
13 :
14 0 : void gui_clipboard_init ( gui_clipboard_t *this_,
15 : GdkClipboard *clipboard,
16 : gui_toolbox_t *tool_switcher,
17 : gui_simple_message_to_user_t *message_to_user,
18 : data_database_reader_t *db_reader,
19 : ctrl_controller_t *controller )
20 : {
21 0 : U8_TRACE_BEGIN();
22 0 : assert( NULL != clipboard );
23 0 : assert( NULL != message_to_user );
24 0 : assert( NULL != db_reader );
25 0 : assert( NULL != controller );
26 :
27 0 : (*this_).destination_diagram_id = DATA_ROW_ID_VOID;
28 0 : (*this_).message_to_user = message_to_user;
29 0 : (*this_).tool_switcher = tool_switcher;
30 0 : (*this_).the_clipboard = clipboard;
31 0 : (*this_).clipboard_stringbuf = utf8stringbuf_init( sizeof((*this_).private_clipboard_buffer),
32 0 : (*this_).private_clipboard_buffer
33 : );
34 :
35 0 : io_exporter_light_init ( &((*this_).exporter), db_reader );
36 0 : io_importer_init ( &((*this_).importer), db_reader, controller );
37 :
38 0 : U8_TRACE_END();
39 0 : }
40 :
41 0 : void gui_clipboard_destroy ( gui_clipboard_t *this_ )
42 : {
43 0 : U8_TRACE_BEGIN();
44 :
45 0 : io_exporter_light_destroy ( &((*this_).exporter) );
46 0 : io_importer_destroy ( &((*this_).importer) );
47 :
48 0 : (*this_).the_clipboard = NULL;
49 0 : (*this_).tool_switcher = NULL;
50 0 : (*this_).message_to_user = NULL;
51 :
52 0 : U8_TRACE_END();
53 0 : }
54 :
55 0 : int gui_clipboard_copy_set_to_clipboard( gui_clipboard_t *this_, const data_small_set_t *set_to_be_copied, data_stat_t *io_stat )
56 : {
57 0 : U8_TRACE_BEGIN();
58 0 : assert( NULL != set_to_be_copied );
59 0 : assert( NULL != io_stat );
60 : int serialize_error;
61 :
62 0 : serialize_error = io_exporter_light_export_set_to_buf( &((*this_).exporter),
63 : set_to_be_copied,
64 : io_stat,
65 : (*this_).clipboard_stringbuf
66 : );
67 :
68 0 : if ( serialize_error == 0 )
69 : {
70 0 : gdk_clipboard_set_text( (*this_).the_clipboard, utf8stringbuf_get_string( (*this_).clipboard_stringbuf ) );
71 : }
72 : else
73 : {
74 0 : U8_LOG_ERROR_HEX( "Exporting selected set to clipboard failed:", serialize_error );
75 : }
76 0 : U8_TRACE_INFO( utf8stringbuf_get_string( (*this_).clipboard_stringbuf ) );
77 :
78 0 : U8_TRACE_END_ERR( serialize_error );
79 0 : return serialize_error;
80 : }
81 :
82 0 : void gui_clipboard_request_clipboard_text( gui_clipboard_t *this_, data_row_id_t destination_diagram_id )
83 : {
84 0 : U8_TRACE_BEGIN();
85 :
86 0 : utf8stringbuf_clear( (*this_).clipboard_stringbuf );
87 :
88 0 : (*this_).destination_diagram_id = destination_diagram_id;
89 0 : U8_TRACE_INFO_INT ( "(*this_).destination_diagram_id:", destination_diagram_id );
90 :
91 0 : gdk_clipboard_read_text_async( (*this_).the_clipboard,
92 : NULL, /* GCancellable* cancellable */
93 : (GAsyncReadyCallback) gui_clipboard_clipboard_text_received_callback,
94 : this_
95 : );
96 :
97 0 : U8_TRACE_END();
98 0 : }
99 :
100 0 : void gui_clipboard_clipboard_text_received_callback( GObject *source_object,
101 : GAsyncResult* res,
102 : gpointer user_data )
103 : {
104 0 : U8_TRACE_BEGIN();
105 0 : assert( NULL != source_object );
106 0 : assert( NULL != res );
107 0 : assert( NULL != user_data );
108 0 : gui_clipboard_t *this_ = user_data;
109 0 : assert( GDK_CLIPBOARD(source_object) == (*this_).the_clipboard );
110 :
111 0 : GError* error = NULL;
112 0 : char* clipboard_text = gdk_clipboard_read_text_finish( (*this_).the_clipboard, res, &error );
113 :
114 0 : if ( error != NULL )
115 : {
116 0 : U8_LOG_ERROR_STR( "Error:", (*error).message );
117 0 : g_error_free( error );
118 : }
119 :
120 0 : if ( clipboard_text != NULL )
121 : {
122 0 : gui_clipboard_private_copy_clipboard_to_db( this_, clipboard_text );
123 : /* g_object_unref( clipboard_text ); */
124 0 : g_free( clipboard_text );
125 : }
126 : else
127 : {
128 0 : gui_simple_message_to_user_show_message( (*this_).message_to_user,
129 : GUI_SIMPLE_MESSAGE_TYPE_ERROR,
130 : GUI_SIMPLE_MESSAGE_CONTENT_NO_INPUT_DATA
131 : );
132 : }
133 :
134 0 : U8_TRACE_TIMESTAMP();
135 0 : U8_TRACE_END();
136 0 : }
137 :
138 0 : void gui_clipboard_private_copy_clipboard_to_db( gui_clipboard_t *this_, const char *json_text )
139 : {
140 0 : U8_TRACE_BEGIN();
141 0 : assert( NULL != json_text );
142 0 : U8_TRACE_INFO_INT ( "(*this_).destination_diagram_id:", (*this_).destination_diagram_id );
143 :
144 : /* import the clipboard */
145 : u8_error_t parse_error;
146 : data_stat_t stat;
147 0 : data_stat_init( &stat );
148 : u8_error_info_t err_info;
149 0 : parse_error = io_importer_import_clipboard( &((*this_).importer),
150 : json_text,
151 : (*this_).destination_diagram_id,
152 : &stat,
153 : &err_info
154 : );
155 :
156 : /* in case a new diagram was pasted, go to nav mode */
157 0 : if ( 0 != data_stat_get_count( &stat, DATA_STAT_TABLE_DIAGRAM, DATA_STAT_SERIES_CREATED ) )
158 : {
159 0 : gui_toolbox_set_selected_tool( (*this_).tool_switcher, GUI_TOOL_NAVIGATE );
160 : }
161 : else
162 : {
163 0 : gui_toolbox_set_selected_tool( (*this_).tool_switcher, GUI_TOOL_EDIT );
164 : }
165 :
166 : /* show a message to the user */
167 0 : if ( u8_error_info_is_error( &err_info ) )
168 : {
169 0 : gui_simple_message_to_user_show_error_info( (*this_).message_to_user, &err_info );
170 : }
171 0 : else if ( U8_ERROR_NONE != parse_error )
172 : {
173 0 : gui_simple_message_to_user_show_message( (*this_).message_to_user,
174 : GUI_SIMPLE_MESSAGE_TYPE_ERROR,
175 : GUI_SIMPLE_MESSAGE_CONTENT_NO_INPUT_DATA
176 : );
177 : }
178 : else
179 : {
180 0 : gui_simple_message_to_user_show_message_with_stat( (*this_).message_to_user,
181 : GUI_SIMPLE_MESSAGE_TYPE_INFO,
182 : GUI_SIMPLE_MESSAGE_CONTENT_PASTE_FROM_CLIPBOARD,
183 : &stat
184 : );
185 : }
186 :
187 0 : data_stat_destroy( &stat );
188 0 : U8_TRACE_END();
189 0 : }
190 :
191 :
192 : /*
193 : Copyright 2016-2024 Andreas Warnke
194 :
195 : Licensed under the Apache License, Version 2.0 (the "License");
196 : you may not use this file except in compliance with the License.
197 : You may obtain a copy of the License at
198 :
199 : http://www.apache.org/licenses/LICENSE-2.0
200 :
201 : Unless required by applicable law or agreed to in writing, software
202 : distributed under the License is distributed on an "AS IS" BASIS,
203 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
204 : See the License for the specific language governing permissions and
205 : limitations under the License.
206 : */
|