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
|
/*
* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; version 2 of the
* License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "stdafx.h"
#include "wb_editor_storednote.h"
#include "grts/structs.workbench.physical.h"
#include "grt/exceptions.h"
#include "grtui/file_charset_dialog.h"
#include "base/string_utilities.h"
using namespace base;
StoredNoteEditorBE::StoredNoteEditorBE(bec::GRTManager *grtm, const GrtStoredNoteRef ¬e)
: bec::BaseEditor(grtm, note), _note(note)
{
}
bool StoredNoteEditorBE::is_script()
{
return _note.is_instance(db_Script::static_class_name());
}
Sql_editor::Ref StoredNoteEditorBE::get_sql_editor()
{
if (!_sql_editor)
{
workbench_physical_ModelRef model(workbench_physical_ModelRef::cast_from(_note->owner()));
_sql_editor= Sql_editor::create(model->rdbms());
}
return _sql_editor;
}
void StoredNoteEditorBE::set_text(const std::string &text)
{
bool flag;
if (get_text(flag) != text)
{
//XXX replace this using module wrapper class
grt::Module *module= get_grt()->get_module("Workbench");
if (!module)
throw std::runtime_error("Workbench module not found");
grt::BaseListRef args(get_grt());
args.ginsert(_note->filename());
args.ginsert(grt::StringRef(text));
module->call_function("setAttachedFileContents", args);
_note->lastChangeDate(bec::fmttime(0, DATETIME_FMT));
}
}
std::string StoredNoteEditorBE::get_text(bool &isutf8)
{
//XXX replace this using module wrapper class
grt::Module *module= get_grt()->get_module("Workbench");
if (!module)
throw std::runtime_error("Workbench module not found");
grt::BaseListRef args(get_grt());
args.ginsert(_note->filename());
grt::StringRef value(grt::StringRef::cast_from(module->call_function("getAttachedFileContents", args)));
if (!g_utf8_validate(value.c_str(), strlen(value.c_str()), NULL))
{
isutf8= false;
return "";
}
isutf8= true;
return *value;
}
void StoredNoteEditorBE::set_name(const std::string &name)
{
if (_note->name() != name)
{
workbench_physical_ModelRef model(workbench_physical_ModelRef::cast_from(_note->owner()));
if (!model.is_valid())
throw std::logic_error("Note owner not set");
grt::ListRef<GrtStoredNote> notes(model->notes());
for (size_t c= notes.count(), i= 0; i < c; i++)
{
GrtStoredNoteRef note(notes[i]);
if (note != _note && *note->name() == name)
throw bec::validation_error(_("Duplicate note name."));
}
bec::AutoUndoEdit undo(this, _note, "name");
_note->name(name);
undo.end(strfmt(_("Rename '%s' to '%s'"), _note->name().c_str(), name.c_str()));
}
}
std::string StoredNoteEditorBE::get_name()
{
return _note->name();
}
bool StoredNoteEditorBE::load_file(const std::string &name)
{
gchar *contents;
gsize length;
if (g_file_get_contents(name.c_str(), &contents, &length, NULL))
{
std::string converted;
if (FileCharsetDialog::ensure_filedata_utf8(contents, length, "",
name,
converted))
{
set_text(converted);
g_free(contents);
return true;
}
g_free(contents);
}
return false;
}
bool StoredNoteEditorBE::save_file(const std::string &name)
{
grt::Module *module= get_grt()->get_module("Workbench");
if (!module)
throw std::runtime_error("Workbench module not found");
grt::BaseListRef args(get_grt());
args.ginsert(_note->filename());
args.ginsert(grt::StringRef(name));
grt::IntegerRef value(grt::IntegerRef::cast_from(module->call_function("exportAttachedFileContents", args)));
return *value == 0;
}
|