File: filemanager.c

package info (click to toggle)
motif 2.3.8-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 36,432 kB
  • sloc: ansic: 452,643; sh: 4,613; makefile: 2,030; yacc: 1,604; lex: 352; cpp: 348
file content (236 lines) | stat: -rw-r--r-- 6,846 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Motif
 *
 * Copyright (c) 1987-2012, The Open Group. All rights reserved.
 *
 * These libraries and programs are free software; you can
 * redistribute them and/or modify them under the terms of the GNU
 * Lesser General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * These libraries and programs are distributed in the hope that
 * they will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with these librararies and programs; if not, write
 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA 02110-1301 USA
 * 
 */
/*
 * HISTORY
 */

#ifdef REV_INFO
#ifndef lint
static char rcsid[] = "$XConsortium: filemanager.c /main/6 1995/07/14 09:41:39 drk $"
#endif
#endif

#include <stdlib.h>
#include <stdio.h>
#include <Xm/Xm.h>           /* Motif Toolkit */
#include <Xm/Container.h>
#include "filemanager.h"

Display  	*display;
XtAppContext  	app_context;
Widget toplevel, mainW, dirOM, fileviewer;
Widget gotoDialog, displayLabel;

static int ErrorHandler(Display *display, XErrorEvent *event);
static void UpdateDir(XtPointer, XtIntervalId*);

#define APP_CLASS "XmdFilemanager"
time_t ltm = 0;
unsigned int updateTime;
char *deleteCommand;
XrmQuark app_class_quark;
XrmQuark app_quark;

char * fallback_resources[] = {
"*updateTime:				2000", /* Check on directory every two seconds */
"*deleteCommand:			rm %s",
"*types.o.largeIcon:			obj.xpm",
"*types.c.largeIcon:			code.xpm",
"*types.h.largeIcon:			code.xpm",
"*types.xpm.largeIcon:			pix.xpm",
"*types.xpm.smallIcon:			s_pix.xpm",
"*types.xpm.action:			vueicon -f %s",
"*types.filemanager.action:		%s",
"*types.default_file.largeIcon:		file.xpm",
"*types.default_file.smallIcon:		s_file.xpm",
"*types.default_dir.largeIcon:		dir.xpm",
"*types.default_dir.smallIcon:		s_dir.xpm",
"*types.default_exec.largeIcon:		exec.xpm",
"*types.default_exec.smallIcon:		s_exec.xpm",
"*types.default_none.largeIcon:		none.xpm",
"*types.default_none.smallIcon:		s_none.xpm",
"*types.default_file.action:		$EDITOR %s",
"*types.default_exec.action:		xterm -e %s",
"*XmContainer.OutlineButton.shadowThickness: 0",
/* "*XmContainer.OutlineButton*armPixmap:	arm.xpm", */
"*XmContainer*foreground:		black",
"*XmContainer*background:		gray90",
"*XmContainer.selectColor:		lightblue",
"*XmContainer.outlineIndentation:	30",
"*XmContainer.outlineColumnWidth:	6cm",
"*XmContainer.detailTabList:		+3cm,+3cm,+3cm",
"*XmIconGadget.renderTable:		*helvetica-bold-r-*-10-*",
"*XmIconGadget.shadowThickness:		0",
"*show_hidden.labelString:		Show Hidden Files",
"*show_hidden.indicatorOn:		XmINDICATOR_CHECK",
"*show_hidden.indicatorSize:		15",
"*help_manager.helpFile:		filemanager",
NULL
};

/*
 *  Main program
 */
int
main(int argc, char* argv[])
{
  Arg		args[10];
  int 		n = 0;
  Pixel		fg, bg;
  Display	*display;
  int		time;
  char		*str;
  int 		i;

  XtSetArg(args[n], XmNallowShellResize, True);  n++;
  toplevel = XtVaAppInitialize(&app_context, APP_CLASS, NULL, 0, &argc, argv,
			       fallback_resources, NULL);

  app_class_quark = XrmStringToQuark(APP_CLASS);
  app_quark = XrmStringToQuark(argv[0]);

  mainW = CreateInterface("main", toplevel);

  /* First try the resource database,  then use the fallbacks below */
  str = XGetDefault(XtDisplay(toplevel), APP_CLASS, "updateTime");
  if (str == NULL)
    str = XGetDefault(XtDisplay(toplevel), argv[0], "updateTime");
  if (str == NULL)
    updateTime = 2000;
  else
    updateTime = atoi(str);

  str = XGetDefault(XtDisplay(toplevel), APP_CLASS, "deleteCommand");
  if (str == NULL)
    str = XGetDefault(XtDisplay(toplevel), argv[0], "deleteCommand");
  if (str == NULL)
    deleteCommand = "rm %s";
  else
    deleteCommand = str;

  /* Find the widgets in the hierarchy */
  dirOM = XtNameToWidget(mainW, "*dirOM");

  dirLabel = (Widget *) XtMalloc(sizeof(Widget) * ndirLabel);
  for (i = 0; i < ndirLabel; i++) {
    char buf[16];
    sprintf(buf, "*l%d", i);
    dirLabel[i] = XtNameToWidget(mainW, buf);
    paths[i] = NULL;
  }

  fileviewer = XtNameToWidget(mainW, "*container");
  gotoDialog = XtNameToWidget(mainW, "*gotoDialog");
  
  displayLabel = XtNameToWidget(mainW, "*Where");

  XtVaGetValues(fileviewer, XmNforeground, &bg,	
		XmNbackground, &fg, NULL, NULL);

  XtManageChild(mainW);

  /* Install our special error handler */
  XSetErrorHandler((XErrorHandler) ErrorHandler);

  /* Add the UTM callbacks on the container area */
  XtAddCallback(fileviewer, XmNdestinationCallback, 
		(XtCallbackProc) targetDestinationCallback, NULL);
  XtAddCallback(fileviewer, XmNconvertCallback, 
		(XtCallbackProc) targetConvertCallback, NULL);
  XtAddCallback(XtParent(fileviewer), XmNresizeCallback,
		fixViewerSize, NULL);

  readdirCB(fileviewer, ".", NULL);

  XtRealizeWidget(toplevel);

  XmContainerRelayout(fileviewer);

  XtAppAddTimeOut(app_context, updateTime,
		  UpdateDir, (XtPointer) 0);

  XtAppMainLoop(app_context);

  return 0;    /* make compiler happy */
}

/* Adjust the size of the underlying container widget to be
   at least large enough to fit in the clipwindow */
void 
fixViewerSize(Widget w, XtPointer i1, XtPointer i2) 
{
  Dimension width, height, container_w, container_h;

  /* Get the correct dimensions of the clipwindow */
  XtVaGetValues(w, XtNwidth, &width, XtNheight, &height, NULL, NULL);

  /* Get the correct dimensions of the container */
  XtVaGetValues(fileviewer, XtNwidth, &container_w,
		XtNheight, &container_h, NULL, NULL);

  XtVaSetValues(fileviewer, XtNwidth, width, NULL, NULL);
  if (container_h < height) 
    XtVaSetValues(fileviewer, XtNheight, height, NULL, NULL);
}

/* Error handler for X protocol errors.  Continue after error */

static int 
ErrorHandler(Display *display, XErrorEvent *event)
{
  char errortext[100];
  XmString tmp;

  XGetErrorText(display, event -> error_code, errortext, 100);

  printf("X Protocol error: %s\n", errortext);
  printf("XID %ld serial %ld major %d minor %d\n",
	 event -> resourceid,
	 event -> serial,
	 event -> request_code,
	 event -> minor_code);

  return 0;
}

/* Check for modification of current directory.  We don't handle updates
   in subdirectories (at least yet),  but this *is* free software */
static void 
UpdateDir(XtPointer data, XtIntervalId *id) 
{
  struct stat buf;

  XtAppAddTimeOut(app_context, updateTime, 
		  (XtTimerCallbackProc) UpdateDir, 0);

  if (stat(currentdir, &buf) != 0) {
    printf("Can't stat directory: %s\n", currentdir);
    return;
  }

  if (ltm != buf.st_ctime) {
    read_directory((Widget) NULL, ".");
  }
}