Convert uses of XKeycodeToKeysym (deprecated) to XkbKeycodeToKeysym
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / Managers / GlobalHistoryMgr.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 libraries 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: GlobalHistoryMgr.cc /main/5 1996/07/10 09:38:04 rcs $
25  *
26  * Copyright (c) 1993 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 #include <sstream>
50 using namespace std;
51 #include "UAS.hh"
52
53 #define C_GlobalHistoryMgr
54 #define C_PrefMgr
55 #define L_Managers
56
57 #define C_xList
58 #define L_Support
59
60 #define C_NodeHistoryAgent
61 #define L_Agents
62
63 #include <Prelude.h>
64
65 LONG_LIVED_CC(GlobalHistoryMgr,global_history_mgr);
66
67 class NodeRecorder : public UAS_Receiver<UAS_DocumentRetrievedMsg>
68 {
69 public:
70   NodeRecorder()
71     { UAS_Common::request ((UAS_Receiver<UAS_DocumentRetrievedMsg> *)this); }
72 private:
73   void receive (UAS_DocumentRetrievedMsg &message, void *client_data);
74 };
75
76   
77 void
78 NodeRecorder::receive (UAS_DocumentRetrievedMsg &message, void *client_data)
79 {
80   extern bool g_style_sheet_update ;
81   if (!g_style_sheet_update)
82     global_history_mgr().add (message.fDoc);
83 }
84
85 static NodeRecorder node_recorder;
86
87 // /////////////////////////////////////////////////////////////////
88 // class constructor
89 // /////////////////////////////////////////////////////////////////
90
91 GlobalHistoryMgr::GlobalHistoryMgr()
92 : f_max_length (pref_mgr().get_int (PrefMgr::NodeHistSize)),
93   f_history_agent (NULL)
94 {
95   // It should default to 100 from the resource file, but just in case: 
96   if (f_max_length <= 0)
97     f_max_length = 50;
98   UAS_Common::request ((UAS_Receiver<UAS_LibraryDestroyedMsg> *) this);
99 }
100
101
102 GlobalHistoryMgr::~GlobalHistoryMgr()
103 {
104   List_Iterator<UAS_Pointer<UAS_Common> > hl (f_history_list);
105   while (hl)
106     f_history_list.remove (hl);
107   delete f_history_agent;
108 }
109
110
111 // /////////////////////////////////////////////////////////////////
112 // display - display the agent
113 // /////////////////////////////////////////////////////////////////
114
115 void
116 GlobalHistoryMgr::display()
117 {
118   if (f_history_agent == NULL)
119     f_history_agent = new NodeHistoryAgent();
120   f_history_agent->display();
121 }
122
123
124 // /////////////////////////////////////////////////////////////////
125 // set_max_length - reset the maximum length
126 // /////////////////////////////////////////////////////////////////
127
128 void
129 GlobalHistoryMgr::set_max_length (int new_length)
130 {
131   f_max_length = new_length;
132
133   // Need to remove some entries if the current list is too long. 
134   if (f_history_list.length() > f_max_length)
135     {
136       List_Iterator<UAS_Pointer<UAS_Common> > hl (f_history_list);
137       int count = f_history_list.length() - f_max_length;
138
139       ON_DEBUG (printf ("GlobalHistoryMgr: removing %d excess\n", count));
140       HistoryDelete delmsg;
141       delmsg.f_index = 0;
142       delmsg.f_count = count;
143       UAS_Sender<HistoryDelete>::send_message (delmsg, 0);
144
145       while (count > 0)
146         {
147           f_history_list.remove (hl);
148           count--;
149         }
150     }
151 }
152
153
154 // /////////////////////////////////////////////////////////////////
155 // add - add a node to the history list
156 // /////////////////////////////////////////////////////////////////
157
158 void
159 GlobalHistoryMgr::add (UAS_Pointer<UAS_Common> &node_ptr)
160 {
161   // First see if the node is already in the history list.
162   List_Iterator<UAS_Pointer<UAS_Common> > hl (f_history_list);
163   int i;
164
165   for (i = 0; hl; hl++, i++)
166     {
167       ON_DEBUG (printf ("%2d: Checking <%s>\n", i, (char*)hl.item()->title()));
168       if (hl.item() == node_ptr)
169           break;
170     }
171
172   if (hl)  // Item exists, so remove it first. 
173     {
174       HistoryDelete delmsg;
175       delmsg.f_index = i;
176       delmsg.f_count = 1;
177       delmsg.f_moving = TRUE;
178       UAS_Sender<HistoryDelete>::send_message (delmsg, 0);
179       f_history_list.remove (hl);
180     }
181   else if (f_history_list.length() + 1 > f_max_length)
182     {
183       hl.reset();
184       f_history_list.remove (hl);
185       HistoryDelete delmsg;
186       delmsg.f_index = 0;
187       delmsg.f_count = 1;
188       delmsg.f_moving = FALSE;
189       UAS_Sender<HistoryDelete>::send_message (delmsg, 0);
190     }
191
192   f_history_list.append (node_ptr);
193
194   HistoryAdd addmsg (node_ptr);
195   addmsg.f_moving = hl ? TRUE : FALSE;
196   UAS_Sender<HistoryAdd>::send_message (addmsg, 0);
197 }
198
199 void
200 GlobalHistoryMgr::receive (UAS_LibraryDestroyedMsg &msg, void *client_data) {
201     //
202     //  Go through each element in the list, sending history
203     //  delete messages for all docs contained in the dead library
204     //
205     int curIndex = 0;
206     List_Iterator<UAS_Pointer<UAS_Common> > hl (f_history_list);
207     while (hl) {
208         if (hl.item()->get_library() == msg.fLib) {
209             HistoryDelete delmsg;
210             delmsg.f_index = curIndex;
211             delmsg.f_count = 1;
212             delmsg.f_moving = FALSE;
213             UAS_Sender<HistoryDelete>::send_message (delmsg, 0);
214             f_history_list.remove (hl);
215         } else {
216             curIndex ++;
217             hl ++;
218         }
219     }
220 }