File: document.cc

package info (click to toggle)
gnome-chemistry-utils 0.14.9-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,836 kB
  • ctags: 7,337
  • sloc: cpp: 72,977; sh: 11,381; xml: 6,304; makefile: 1,663; ansic: 1,061
file content (187 lines) | stat: -rw-r--r-- 4,935 bytes parent folder | download
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
// -*- C++ -*-

/*
 * Gnome Chemistry Utils
 * libs/gcu/document.cc
 *
 * Copyright (C) 2004-2011 Jean Bréfort <jean.brefort@normalesup.org>
 *
 * 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; either version 3 of the
 * License, or (at your option) any later version.
 *
 * 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 "config.h"
#include "document.h"
#include "application.h"
#include "residue.h"
#include "dialog.h"
#include <glib/gi18n-lib.h>
#include <cstring>
#include <sstream>

using namespace std;

namespace gcu
{


Document::Document (Application *App): Object (DocumentType),
m_Empty (true)
{
	m_App = App;
	if (m_App)
		m_App->AddDocument (this);
}

Document::~Document ()
{
	if (m_App)
		m_App->RemoveDocument (this);
}

char* Document::GetNewId (char const *id, bool Cache)
{
	gchar *Id = g_strdup (id);
	int i = 0, k;
	while ((Id[i] < '0') || (Id[i] > '9'))
		i++;
	k = atoi (id + i);
	gchar *buf = new gchar[i + 16];
	strncpy (buf, Id, i);
	buf[i] = 0;
	g_free (Id);
	int j = 1;
	string s = m_TranslationTable[buf];
	if (s.size ())
		j = atoi (s.c_str ());
	char* key = g_strdup (buf);
	while (snprintf (buf + i, 16, "%d", j), GetDescendant (buf) != NULL)
		j++;
	Id = g_strdup_printf ("%d", j);
	Object *obj = GetDescendant (id);
	if (obj && (k > 1 || m_NewObjects.find (obj) == m_NewObjects.end ()))
		if (Cache) {
			m_TranslationTable[key] = Id;
			m_TranslationTable[id] = buf;
		}
	g_free (Id);
	g_free (key);
	return buf;
}

Residue *Document::CreateResidue (G_GNUC_UNUSED char const *name, G_GNUC_UNUSED char const *symbol, G_GNUC_UNUSED Molecule *molecule)
{
	return NULL;
}

Residue const *Document::GetResidue (char const *symbol, bool *ambiguous)
{
	return Residue::GetResidue (symbol, ambiguous);
}

bool Document::SetTarget (char const *id, Object **target, Object *parent, Object *owner, Action action) throw (std::runtime_error)
{
	if (target == NULL)
	    throw std::runtime_error ("Can't set a NULL target.");
	if (parent) {
		*target = parent->GetDescendant (id);
		if (*target) {
			if (owner)
				owner->OnLoaded ();
			return true;
		}
	}
	PendingTarget pt;
	pt.target = target;
	pt.parent = parent;
	pt.owner = owner;
	pt.action = action;
	m_PendingTable[id].push_back (pt);
	return false;
}

bool Document::Loaded () throw (LoaderError)
{
	unsigned count = 0;
	std::map <std::string, list <PendingTarget> >::iterator i, end = m_PendingTable.end ();
	std::set <Object *> Deleted;
	std::set <Object *>::iterator new_end = m_NewObjects.end ();
	for (i = m_PendingTable.begin (); i != end; i++) {
		std::string id = (*i).first;
		std::list <PendingTarget> &l = (*i).second;
		std::list <PendingTarget>::iterator j = l.begin (), jend = l.end ();
		Object *obj = (*j).parent->GetDescendant (id.c_str ());
		if (obj == NULL)
			obj = (*j).parent->GetDocument ()->GetDescendant (id.c_str ());
		if (obj && m_NewObjects.find (obj) == new_end)
			obj = NULL;
		if (obj == NULL) {
			switch ((*j).action) {
			case ActionException: {
				m_PendingTable.clear ();
				std::ostringstream str;
				// Note to translators: the two strings are concatenated with the missing id between them.
				str << _("The input contains a reference to object \"") << id << _("\" but no object with this Id is described.");
				throw LoaderError (str.str ());
			}
			case ActionDelete:
				if ((*j).owner) {
					Deleted.insert ((*j).owner);
					delete (*j).owner;
					(*j).owner = NULL;
				}
			case ActionIgnore:
				break;
			}
		} else while (j != jend) {
			std::set <Object *>::iterator d = Deleted.end ();
			if (Deleted.find ((*j).owner) == d) {
				*(*j).target = obj;
				if ((*j).owner)
					(*j).owner->OnLoaded ();
				count++;
			}
			j++;
		}
	}
	m_PendingTable.clear ();
	m_NewObjects.clear ();
	// call OnLoaded for dirty objects
	std::set <Object *>::iterator k, kend = m_DirtyObjects.end ();
	for (k = m_DirtyObjects.begin (); k != kend; k++)
		(*k)->OnLoaded ();
	m_DirtyObjects.clear ();
	m_TranslationTable.clear ();
	return count > 0;
}

void Document::ObjectLoaded (Object *obj)
{
	m_NewObjects.insert (obj);
}

std::string Document::Name ()
{
	return _("Document");
}

std::string& Document::GetTranslatedId (const char* id)
{
	static std::string empty_string ("");
	std::map < std::string, std::string >::iterator i = m_TranslationTable.find (id);
	return (i != m_TranslationTable.end ())? (*i).second: empty_string;
}

}	//	namespace gcu