Link._pike
Go to the documentation of this file.
1 /* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * $Id: Link.pike,v 1.1 2008/03/31 13:39:57 exodusd Exp $
18  */
19 inherit "/classes/Object";
20 #include <macros.h>
21 #include <exception.h>
22 #include <classes.h>
23 #include <types.h>
24 #include <database.h>
25 //! A Link points to some other object inside the sTeam system.
26 class Link : public Object{
27 public:
28 
29 
30 
31 
32 
33 
34  object oLinkObject;
35 
36 protected:
37  void
38 init()
39 {
40  ::init();
41  add_data_storage(STORE_LINK, retrieve_link_data, restore_link_data);
42 }
43 
44 public:
45 
46 /**
47  * Create a duplicate of this link object which means create
48  * another link pointing to the same object than this.
49  *
50  * @return the duplicated object.
51  */
52 mapping do_duplicate(void|mapping vars)
53 {
54  if ( !mappingp(vars) )
55  vars = ([ ]);
56  vars->link_to = oLinkObject;
57  return ::do_duplicate(vars);
58 }
59 
60 protected:
61  void delete_object()
62 {
63  ::delete_object();
64  if ( objectp(oLinkObject) )
65 }
66 
67 public:
68 
69 protected:
70  void
71 create_object()
72 {
73  oLinkObject = 0;
74 }
75 
76 public:
77 
78 
79 
80 /**
81  * Set the link object which is the object this link refers to.
82  *
83  * @param obj - the link
84  * @see query_link_object
85  */
86 final void
87 set_link_object(object obj)
88 {
89  if ( objectp(oLinkObject) || !objectp(obj) )
90  return; // only set link once !
91  /* the object links to another one now */
92  oLinkObject = obj;
93  require_save(STORE_LINK);
94 }
95 
96 /**
97  * Get the object this link points to.
98  *
99  * @return the object linked to this
100  * @see set_link_object
101  */
102 final object
103 get_link_object()
104 {
105  return oLinkObject;
106 }
107 
108 object get_destination()
109 {
110  return get_link_object();
111 }
112 
113 void lowAppendXML(object rootNode, void|int depth)
114 {
115  ::lowAppendXML(rootNode, depth);
116  object dest = get_destination();
117  rootNode->add_prop("Target", (objectp(dest)?
118  (string)dest->get_object_id(): "0"));
119 }
120 
121 
122 /**
123  * Retrieve the to be saved data of this Link.
124  *
125  * @return mapping of link data.
126  */
127 final mapping
128 private:
129 retrieve_link_data()
130 {
131  if ( CALLER != _Database )
132  THROW("Caller is not database !", E_ACCESS);
133  return ([ "LinkObject": oLinkObject ]);
134 }
135 
136 public:
137 
138 /**
139  * Restore the saved link data. Called by database to load the link.
140  *
141  * @param mixed data - the data to be restored.
142  */
143 final void
144 private:
145 restore_link_data(mixed data)
146 {
147  if ( CALLER != _Database )
148  THROW("Caller is not database !", E_ACCESS);
149  if ( arrayp(data) )
150  oLinkObject = data[0];
151  else
152  oLinkObject = data["LinkObject"];
153 }
154 
155 public:
156 
157 
158 /**
159  * Get the action to take for this link. Usually follow for exits
160  * or get if the link points to a document.
161  *
162  * @return the link action string description.
163  */
164 string get_link_action()
165 {
166  if ( !objectp(oLinkObject) )
167  return "none";
168  else if ( oLinkObject->get_object_class() &
169  (CLASS_CONTAINER|CLASS_ROOM|CLASS_EXIT|CLASS_MESSAGEBOARD|CLASS_DOCEXTERN) )
170  return "follow";
171  else
172  return "get";
173 }
174 
175 
176 /**
177  * Get the content size of the object linked to.
178  *
179  * @return the content size of the linked object.
180  */
181 int get_content_size()
182 {
183  if ( objectp(oLinkObject) ) {
184  if ( !(oLinkObject->get_object_class() & CLASS_LINK) )
185  return oLinkObject->get_content_size();
186  }
187  return ::get_content_size();
188 }
189 
190 array stat()
191 {
192  array stat;
193  if ( objectp(oLinkObject) )
194  stat = oLinkObject->stat();
195  else
196  stat = ::stat();
197  return stat;
198 }
199 
200 int get_object_class() { return CLASS_LINK | ::get_object_class(); }
201 
202 
203 
204 
205 
206 
207 
208 
209 };