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