OpenIndiana and Solaris port
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / oliasdb / pref.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  * $XConsortium: pref.cc /main/4 1996/06/11 17:30:27 cde-hal $
25  *
26  * Copyright (c) 1992 HAL Computer Systems International, Ltd.
27  * All rights reserved.  Unpublished -- rights reserved under
28  * the Copyright Laws of the United States.  USE OF A COPYRIGHT
29  * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
30  * OR DISCLOSURE.
31  * 
32  * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
33  * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
34  * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
35  * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
36  * INTERNATIONAL, LTD.
37  * 
38  *                         RESTRICTED RIGHTS LEGEND
39  * Use, duplication, or disclosure by the Government is subject
40  * to the restrictions as set forth in subparagraph (c)(l)(ii)
41  * of the Rights in Technical Data and Computer Software clause
42  * at DFARS 252.227-7013.
43  *
44  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
45  *                  1315 Dell Avenue
46  *                  Campbell, CA  95008
47  * 
48  */
49
50
51
52
53 #include "oliasdb/pref.h"
54
55 MMDB_BODIES(pref)
56
57 pref_smart_ptr::pref_smart_ptr(pref_base* uptr,
58                                const char* name) :  
59         pbase(uptr), pref_set_hd_ptr(uptr -> pref_set_hd),
60         v_name_hd(0), v_cnfg_hd(0) 
61 {
62
63    abs_storage *pref_store = pref_set_hd_ptr -> its_store();
64
65    oid_list_handler* pref_list = (*pref_set_hd_ptr) -> 
66          get_locs(managers::query_mgr -> form_pstring_handler(name), 
67                   BASE_COMPONENT_INDEX
68                  );
69
70
71    if ( pref_list == 0 ) {
72
73 MESSAGE(cerr, "NEW RECORD");
74       try {
75          pbase -> trans().begin();
76    
77          tuple_handler* w =
78              new tuple_handler(oid_t(USER_CONFIG_CODE, 0), pref_store);
79    
80          v_name_hd = new pstring_handler(name, strlen(name), pref_store);
81          v_cnfg_hd = new pstring_handler("", 0, pref_store);
82    
83          (*w) -> pinned_insert(BASE_COMPONENT_INDEX, v_name_hd -> its_oid());
84          (*w) -> pinned_insert(BASE_COMPONENT_INDEX+1, v_cnfg_hd -> its_oid());
85    
86    /////////////////////
87    // init smart_ptr
88    /////////////////////
89          smart_ptr::_init(w->its_oid(), pref_store);
90    
91          (*pref_set_hd_ptr) -> insert_object( *w );
92    
93          pbase -> trans().end();
94       }
95
96       catch (mmdbException&, e)
97       {
98          smart_ptr::_init(ground, 0); // mark the pref obsolete
99          pbase -> trans().rollback();
100          rethrow;
101       }
102       end_try;
103
104
105    } else {
106 MESSAGE(cerr, "OLD RECORD");
107
108       if ( (*pref_list) -> count() == 0 ) {
109          throw(stringException("zero prefs in the list"));
110       }
111
112       smart_ptr::_init((*pref_list) -> operator()(1), pref_store);
113
114       v_name_hd = (pstring_handler*)
115                     get_handler(BASE_COMPONENT_INDEX, STRING_CODE);
116       v_cnfg_hd = (pstring_handler*)
117                     get_handler(BASE_COMPONENT_INDEX+1, STRING_CODE);;
118    }
119
120 }
121
122 pref_smart_ptr::~pref_smart_ptr()
123 {
124    delete v_name_hd;
125    delete v_cnfg_hd;
126 }
127
128 Boolean pref_smart_ptr::update_value(const pstring& new_uc)
129 {
130    return update_value(((pstring&)new_uc).get(), ((pstring&)new_uc).size());
131 }
132
133 Boolean pref_smart_ptr::update_value(const char* new_pref, 
134                                      int new_pref_sz)
135 {
136    try {
137       pbase -> trans().begin();
138
139       (*v_cnfg_hd) -> update(new_pref, new_pref_sz);
140
141       pbase -> trans().end();
142    }
143
144    catch (mmdbException&, e)
145       {
146          pbase -> trans().rollback();
147          return false;
148       }
149    end_try;
150
151
152    return true;
153 }
154
155 Boolean pref_smart_ptr::remove_from_db()
156 {
157    try {
158       pbase -> trans().begin();
159
160       (*pref_set_hd_ptr) -> remove_component(its_oid());
161
162       pbase -> trans().end();
163    }
164
165    catch (mmdbException&, e)
166       {
167          pbase -> trans().rollback();
168          return false;
169       }
170    end_try;
171
172
173    return true;
174 }
175
176 char* pref_smart_ptr::name()
177 {
178    return (*v_name_hd) -> get();
179 }
180
181 pstring* pref_smart_ptr::pref_value()
182 {
183    return v_cnfg_hd -> operator->();
184 }
185