1 // $Id: xxRareModeSavePrompts.cc 4497 2012-05-27 00:33:09Z flaterco $
2 
3 /*  xxRareModeSavePrompts  Get a file name and two timestamps from the
4     user.  If successful, do caller.save (filename, startTime, endTime).
5 
6     Copyright (C) 1998  David Flater.
7 
8     This program is free software: you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "xtide.hh"
23 #include "xxHorizDialog.hh"
24 #include "xxMultiChoice.hh"
25 #include "xxTimestampDialog.hh"
26 #include "xxTimestamp.hh"
27 #include "xxRareModeSavePrompts.hh"
28 #include "xxTextMode.hh"
29 
30 
xxRareModeSavePromptsCallback(Widget w unusedParameter,XtPointer client_data,XtPointer call_data unusedParameter)31 static void xxRareModeSavePromptsCallback (
32                                          Widget w unusedParameter,
33                                          XtPointer client_data,
34                                          XtPointer call_data unusedParameter) {
35   assert (client_data);
36   xxRareModeSavePrompts *prompts ((xxRareModeSavePrompts *)client_data);
37   prompts->callback();
38   delete prompts->dismiss();
39 }
40 
41 
callback()42 void xxRareModeSavePrompts::callback() {
43   Dstr filename (filenameDiag->val());
44   if (filename.strchr ('\n') != -1 ||
45       filename.strchr ('\r') != -1 ||
46       filename.strchr (' ') != -1 ||
47       filename[0] == '-') {
48     Dstr details ("Well, it's not that I can't do it, it's that you probably\n\
49 don't want me to.  The filename that you entered is '");
50     details += filename;
51     details += "'.\n";
52     if (filename[0] == '-')
53       details += "Filenames that begin with a dash are considered harmful.";
54     else
55       details += "Whitespace in filenames is considered harmful.";
56     Global::barf (Error::CANT_OPEN_FILE, details, Error::nonfatal);
57   } else {
58     Dstr startTimeString, endTimeString;
59     beginTimeDiag->val (startTimeString);
60     Timestamp startTime (startTimeString, _timezone);
61     endTimeDiag->val (endTimeString);
62     Timestamp endTime (endTimeString, _timezone);
63     if (startTime.isNull())
64       Global::cant_mktime (startTimeString, _timezone, Error::nonfatal);
65     else if (endTime.isNull())
66       Global::cant_mktime (endTimeString, _timezone, Error::nonfatal);
67     else {
68       if (startTime <= endTime)
69         _caller.save (filename, startTime, endTime);
70       else
71         _caller.save (filename, endTime, startTime);
72     }
73   }
74 }
75 
76 
xxRareModeSavePromptsCancelCallback(Widget w unusedParameter,XtPointer client_data,XtPointer call_data unusedParameter)77 static void xxRareModeSavePromptsCancelCallback (
78                                          Widget w unusedParameter,
79                                          XtPointer client_data,
80                                          XtPointer call_data unusedParameter) {
81   assert (client_data);
82   delete ((xxRareModeSavePrompts *)client_data)->dismiss();
83 }
84 
85 
~xxRareModeSavePrompts()86 xxRareModeSavePrompts::~xxRareModeSavePrompts() {
87   unrealize();
88   _caller.noClose = false;
89 }
90 
91 
xxRareModeSavePrompts(const xxWidget & parent,xxTextMode & caller,constString initFilename,Timestamp initStartTime,Timestamp initEndTime,const Dstr & timezone)92 xxRareModeSavePrompts::xxRareModeSavePrompts (const xxWidget &parent,
93 					      xxTextMode &caller,
94 					      constString initFilename,
95 					      Timestamp initStartTime,
96 					      Timestamp initEndTime,
97 					      const Dstr &timezone):
98   xxWindow (parent, boxContainer, XtGrabExclusive),
99   _caller(caller),
100   _timezone(timezone) {
101 
102   _caller.noClose = true;
103   setTitle ("Save prompts");
104 
105   Arg labelArgs[3] =  {
106     {XtNbackground, (XtArgVal)xxX::pixels[Colors::background]},
107     {XtNforeground, (XtArgVal)xxX::pixels[Colors::foreground]},
108     {XtNborderWidth, (XtArgVal)0}
109   };
110   Arg buttonArgs[4] =  {
111     {XtNvisual, (XtArgVal)xxX::visual},
112     {XtNcolormap, (XtArgVal)xxX::colormap},
113     {XtNbackground, (XtArgVal)xxX::pixels[Colors::button]},
114     {XtNforeground, (XtArgVal)xxX::pixels[Colors::foreground]}
115   };
116 
117   {
118     Dstr helptext ("\
119 Please enter the name of the file in which to save output.\n\
120 You can also change the begin and end times for the listing.\n\
121 The active time zone is ");
122     if (Global::settings["z"].c == 'n')
123       helptext += _timezone;
124     else
125       helptext += "UTC0";
126     helptext += ".";
127     Widget labelWidget = xxX::createXtWidget (helptext.aschar(),
128       labelWidgetClass, container->widget(), labelArgs, 3);
129     helpLabel = xxX::wrap (labelWidget);
130   }
131 
132   filenameDiag = std::auto_ptr<xxHorizDialog> (new xxHorizDialog (
133                                                                 *container,
134 							        "Filename:",
135 							        initFilename));
136   beginTimeDiag = std::auto_ptr<xxTimestampDialog> (new xxTimestampDialog (
137                                                                  *container,
138 							         "Start at:",
139 							         initStartTime,
140 							         _timezone));
141   endTimeDiag = std::auto_ptr<xxTimestampDialog> (new xxTimestampDialog (
142                                                                    *container,
143 							           "End at:",
144 							           initEndTime,
145 							           _timezone));
146 
147   {
148     Widget labelWidget = xxX::createXtWidget ("  ", labelWidgetClass,
149       container->widget(), labelArgs, 3);
150     spaceLabel = xxX::wrap (labelWidget);
151   }{
152     Widget buttonWidget = xxX::createXtWidget ("Go", commandWidgetClass,
153       container->widget(), buttonArgs, 4);
154     XtAddCallback (buttonWidget, XtNcallback, xxRareModeSavePromptsCallback,
155      (XtPointer)this);
156     goButton = xxX::wrap (buttonWidget);
157   }{
158     Widget buttonWidget = xxX::createXtWidget ("Cancel", commandWidgetClass,
159       container->widget(), buttonArgs, 4);
160     XtAddCallback (buttonWidget, XtNcallback,
161       xxRareModeSavePromptsCancelCallback, (XtPointer)this);
162     cancelButton = xxX::wrap (buttonWidget);
163   }
164 
165   realize();
166 }
167 
168 // Cleanup2006 Done
169