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