~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/xpinstall/packager/mac/ASEncoder/src/nsASEApp.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* The contents of this file are subject to the Netscape Public License
 
3
 * Version 1.1 (the "License"); you may not use this file except in
 
4
 * compliance with the License. You may obtain a copy of the License at
 
5
 * http://www.mozilla.org/NPL/ 
 
6
 *
 
7
 * Software distributed under the License is distributed on an "AS IS" basis,
 
8
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
 
9
 * for the specific language governing rights and limitations under the 
 
10
 * License. 
 
11
 * 
 
12
 * The Original Code is Mozilla Communicator client code, released March
 
13
 * 31, 1998. 
 
14
 *
 
15
 * The Initial Developer of the Original Code is Netscape Communications
 
16
 * Corporation. Portions created by Netscape are Copyright (C) 1999
 
17
 * Netscape Communications Corporation. All Rights Reserved.  
 
18
 * 
 
19
 * Contributors:
 
20
 *     Samir Gehani <sgehani@netscape.com>
 
21
 */
 
22
 
 
23
 
 
24
#ifndef _NS_ASEAPP_H_
 
25
        #include "nsASEApp.h"
 
26
#endif
 
27
 
 
28
#include <AppleEvents.h>
 
29
#include <Balloons.h>
 
30
#include <MacTypes.h>
 
31
 
 
32
#include "nsEventHandler.h"
 
33
#include "nsAppleSingleEncoder.h"
 
34
#include "nsAppleSingleDecoder.h"
 
35
#include "MoreFilesExtras.h"
 
36
 
 
37
Boolean gDone;
 
38
 
 
39
nsASEApp::nsASEApp()
 
40
{
 
41
        InitManagers();
 
42
        InitAEHandlers();
 
43
        mWindow = NULL;
 
44
        SetCompletionStatus(false);
 
45
        
 
46
        OSErr err = NavLoad();
 
47
        if (err!= noErr)
 
48
                FatalError(navLoadErr);
 
49
                
 
50
        MakeMenus();
 
51
}
 
52
 
 
53
nsASEApp::~nsASEApp()
 
54
{
 
55
        NavUnload();
 
56
}
 
57
 
 
58
void
 
59
nsASEApp::InitManagers(void)
 
60
{
 
61
        MaxApplZone();
 
62
        MoreMasters(); MoreMasters(); MoreMasters();
 
63
        
 
64
        InitGraf(&qd.thePort);
 
65
        InitFonts();                    
 
66
        InitWindows();
 
67
        InitMenus();
 
68
        TEInit();               
 
69
        InitDialogs(NULL);
 
70
        
 
71
        InitCursor();
 
72
        FlushEvents(everyEvent, 0);     
 
73
}
 
74
 
 
75
#pragma mark -
 
76
#pragma mark *** Apple Event Handlers ***
 
77
#pragma mark -
 
78
 
 
79
pascal OSErr
 
80
EncodeEvent(AppleEvent *appEvent, AppleEvent *reply, SInt32 handlerRefCon)
 
81
{
 
82
        OSErr   err = noErr;
 
83
        FSSpec  param;
 
84
        Boolean result = false, isDir = false;
 
85
        AEDesc  fileDesc;
 
86
        long    dummy;
 
87
        
 
88
        // extract FSSpec from params
 
89
        err = AEGetParamDesc(appEvent, keyDirectObject, typeFSS, &fileDesc);
 
90
        if (err != noErr)
 
91
                goto reply;
 
92
        BlockMoveData(*fileDesc.dataHandle, &param, sizeof(FSSpec));
 
93
        
 
94
        // param check
 
95
        err = nsASEApp::GotRequiredParams(appEvent);
 
96
        if (err != noErr)
 
97
                goto reply;
 
98
                
 
99
        FSpGetDirectoryID(&param, &dummy, &isDir);
 
100
        
 
101
        // if folder recursively traverse and encode contents
 
102
        if (isDir)
 
103
        {
 
104
                nsAppleSingleEncoder encoder;
 
105
                err = encoder.EncodeFolder(&param);
 
106
        }
 
107
        else
 
108
        {
 
109
                // it's a file not a folder so proceed as usual
 
110
                
 
111
                // check if given file has res fork (takes care of existence check)
 
112
                if (nsAppleSingleEncoder::HasResourceFork(&param))
 
113
                {
 
114
                        // encode given file
 
115
                        nsAppleSingleEncoder encoder;
 
116
                        err = encoder.Encode(&param);
 
117
                }
 
118
        }
 
119
        
 
120
        // if noErr thus far 
 
121
        if (err == noErr)
 
122
        {
 
123
                // then set result to true
 
124
                result = true;
 
125
        }
 
126
        
 
127
reply:
 
128
        // package reply
 
129
        AEPutParamPtr(reply, keyDirectObject, typeBoolean, &result, sizeof(result));
 
130
        
 
131
        // boolean takes care of failures
 
132
        return noErr;
 
133
}
 
134
 
 
135
pascal OSErr
 
136
DecodeEvent(AppleEvent *appEvent, AppleEvent *reply, SInt32 handlerRefCon)
 
137
{
 
138
        OSErr   err = noErr;
 
139
        FSSpec  param, outFile;
 
140
        Boolean result = false, isDir = false;
 
141
        AEDesc  fileDesc;
 
142
        long    dummy;
 
143
        
 
144
        // extract FSSpec from params
 
145
        err = AEGetParamDesc(appEvent, keyDirectObject, typeFSS, &fileDesc);
 
146
        if (err != noErr)
 
147
                goto reply;
 
148
        BlockMoveData(*fileDesc.dataHandle, &param, sizeof(FSSpec));
 
149
        
 
150
        // param check
 
151
        err = nsASEApp::GotRequiredParams(appEvent);
 
152
        if (err != noErr)
 
153
                goto reply;
 
154
                        
 
155
        FSpGetDirectoryID(&param, &dummy, &isDir);
 
156
        
 
157
        // if folder recursively traverse and encode contents
 
158
        if (isDir)
 
159
        {
 
160
                nsAppleSingleDecoder decoder;
 
161
                err = decoder.DecodeFolder(&param);
 
162
        }
 
163
        else
 
164
        {       
 
165
                // it's a file not a folder so proceed as usual
 
166
                
 
167
                // check if given file is in AS format (takes care of existence check)
 
168
                if (nsAppleSingleDecoder::IsAppleSingleFile(&param))
 
169
                {
 
170
                        // decode given file
 
171
                        nsAppleSingleDecoder decoder;
 
172
                        err = decoder.Decode(&param, &outFile);
 
173
                }
 
174
        }
 
175
        
 
176
        // if noErr thus far 
 
177
        if (err == noErr)
 
178
        {
 
179
                // then set result to true
 
180
                result = true;
 
181
        }
 
182
        
 
183
reply:
 
184
        // package reply
 
185
        AEPutParamPtr(reply, keyDirectObject, typeBoolean, &result, sizeof(result));
 
186
        
 
187
        // boolean takes care of failures
 
188
        return noErr;
 
189
}
 
190
 
 
191
pascal OSErr
 
192
QuitEvent(AppleEvent *appEvent, AppleEvent *reply, SInt32 handlerRefCon)
 
193
{
 
194
        OSErr   err = noErr;
 
195
        
 
196
        nsASEApp::SetCompletionStatus(true);
 
197
        
 
198
        return err;
 
199
}
 
200
 
 
201
#pragma mark -
 
202
 
 
203
void
 
204
nsASEApp::InitAEHandlers()
 
205
{
 
206
        OSErr                                   err = noErr;
 
207
        
 
208
        mEncodeUPP = NewAEEventHandlerProc((ProcPtr) EncodeEvent);
 
209
        err = AEInstallEventHandler(kASEncoderEventClass, kAEEncode,
 
210
                                                                mEncodeUPP, 0L, false);
 
211
        if (err != noErr)
 
212
                ::CautionAlert(aeInitErr, nil);
 
213
        
 
214
        mDecodeUPP = NewAEEventHandlerProc((ProcPtr) DecodeEvent);
 
215
        err = AEInstallEventHandler(kASEncoderEventClass, kAEDecode,
 
216
                                                                mDecodeUPP, 0L, false);
 
217
        if (err != noErr)
 
218
                ::CautionAlert(aeInitErr, nil);
 
219
                
 
220
        mQuitUPP = NewAEEventHandlerProc((ProcPtr) QuitEvent);
 
221
        err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
 
222
                                                                mQuitUPP, 0L, false);
 
223
        if (err != noErr)
 
224
                ::CautionAlert(aeInitErr, nil);
 
225
}
 
226
OSErr
 
227
nsASEApp::GotRequiredParams(AppleEvent *appEvent)
 
228
{
 
229
        OSErr           err = noErr;
 
230
        DescType        returnedType;
 
231
        Size            actualSize;
 
232
 
 
233
        err = AEGetAttributePtr(appEvent, keyMissedKeywordAttr, typeWildCard,
 
234
                                                        &returnedType, NULL, 0, &actualSize);
 
235
 
 
236
        if (err == errAEDescNotFound)
 
237
                err = noErr;
 
238
        else if (err == noErr)
 
239
                err = errAEParamMissed;
 
240
                
 
241
        return err;
 
242
}
 
243
 
 
244
void
 
245
nsASEApp::MakeMenus()
 
246
{
 
247
    Handle              mbarHdl;
 
248
        MenuHandle      menuHdl;
 
249
 
 
250
        mbarHdl = ::GetNewMBar(rMenuBar);
 
251
        ::SetMenuBar(mbarHdl);
 
252
        
 
253
        if ((menuHdl = ::GetMenuHandle(rMenuApple))!=nil) 
 
254
        {
 
255
                ::AppendResMenu(menuHdl, 'DRVR');
 
256
        }
 
257
                
 
258
        if ((menuHdl = GetMenuHandle(rMenuEdit))!=nil)
 
259
                ::DisableItem(menuHdl, 0);
 
260
        
 
261
        ::HMGetHelpMenuHandle(&menuHdl);
 
262
        ::DisableItem(menuHdl, 0);
 
263
 
 
264
        ::DrawMenuBar();
 
265
}
 
266
 
 
267
void
 
268
nsASEApp::SetCompletionStatus(Boolean aVal)
 
269
{
 
270
        gDone = aVal;
 
271
}
 
272
 
 
273
Boolean 
 
274
nsASEApp::GetCompletionStatus()
 
275
{
 
276
        return gDone;
 
277
}
 
278
 
 
279
 
 
280
 
 
281
void
 
282
nsASEApp::FatalError(short aErrID)
 
283
{
 
284
        ::StopAlert(aErrID, nil);
 
285
        SetCompletionStatus(true);
 
286
}
 
287
 
 
288
OSErr
 
289
nsASEApp::Run()
 
290
{
 
291
        OSErr           err = noErr;
 
292
        EventRecord evt;
 
293
        nsEventHandler handler;
 
294
        
 
295
        while (!gDone)
 
296
        {
 
297
                if (::WaitNextEvent(everyEvent, &evt, 180, NULL))
 
298
                {
 
299
                        handler.HandleNextEvent(&evt);
 
300
                }
 
301
        }
 
302
                
 
303
        return err;
 
304
}
 
305
 
 
306
int
 
307
main(void)
 
308
{
 
309
        nsASEApp app;
 
310
        
 
311
        app.Run();
 
312
        
 
313
        return 0;
 
314
}
 
315