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