1: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2: /* ***** BEGIN LICENSE BLOCK *****
3: * Version: NPL 1.1/GPL 2.0/LGPL 2.1
4: *
5: * The contents of this file are subject to the Netscape Public License
6: * Version 1.1 (the "License"); you may not use this file except in
7: * compliance with the License. You may obtain a copy of the License at
8: * http://www.mozilla.org/NPL/
9: *
10: * Software distributed under the License is distributed on an "AS IS" basis,
11: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12: * for the specific language governing rights and limitations under the
13: * License.
14: *
15: * The Original Code is mozilla.org code.
16: *
17: * The Initial Developer of the Original Code is
18: * Netscape Communications Corporation.
19: * Portions created by the Initial Developer are Copyright (C) 2002
20: * the Initial Developer. All Rights Reserved.
21: *
22: * Contributor(s):
23: *
24: * Alternatively, the contents of this file may be used under the terms of
25: * either the GNU General Public License Version 2 or later (the "GPL"), or
26: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27: * in which case the provisions of the GPL or the LGPL are applicable instead
28: * of those above. If you wish to allow use of your version of this file only
29: * under the terms of either the GPL or the LGPL, and not to allow others to
30: * use your version of this file under the terms of the NPL, indicate your
31: * decision by deleting the provisions above and replace them with the notice
32: * and other provisions required by the GPL or the LGPL. If you do not delete
33: * the provisions above, a recipient may use your version of this file under
34: * the terms of any one of the NPL, the GPL or the LGPL.
35: *
36: * ***** END LICENSE BLOCK ***** */
37:
38: #import "MainController.h"
39: #import "BrowserWindowController.h"
40: #include "BookmarksService.h"
41: #include "nsCOMPtr.h"
42: #include "nsIServiceManager.h"
43: #include "nsIIOService.h"
44: #include "nsCocoaBrowserService.h"
45: #import "AboutBox.h"
46:
47:
48: static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
49:
50: @implementation MainController
51:
52: -(id)init
53: {
54: if ( (self = [super init]) ) {
55: mSplashScreen = [[SplashScreenWindow alloc] splashImage:nil withFade:YES withStatusRect:NSMakeRect(0,0,0,0)];
56: }
57: mFindDialog = nil;
58: mMenuBookmarks = nil;
59: return self;
60: }
61:
62: -(void)dealloc
63: {
64: [super dealloc];
65: [mFindDialog release];
66: printf("Main controller died.\n");
67: }
68:
69: -(void)awakeFromNib
70: {
71: [self newWindow: self];
72:
73: [mSplashScreen close];
74:
75: [mBookmarksMenu setAutoenablesItems: NO];
76: mMenuBookmarks = new BookmarksService(nil);
77: mMenuBookmarks->AddObserver();
78: mMenuBookmarks->ConstructBookmarksMenu(mBookmarksMenu, nsnull);
79: BookmarksService::gMainController = self;
80:
81: // Initialize offline mode.
82: mOffline = NO;
83: nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
84: if (!ioService)
85: return;
86: PRBool offline = PR_FALSE;
87: ioService->GetOffline(&offline);
88: mOffline = offline;
89:
90: // Set the menu item's text to "Go Online" if we're currently
91: // offline.
92: if (mOffline)
93: [mOfflineMenuItem setTitle: @"Go Online"];
94:
95: }
96:
97: -(IBAction)newWindow:(id)aSender
98: {
99: // If we have a key window, have it autosave its dimensions before
100: // we open a new window. That ensures the size ends up matching.
101: NSWindow* keyWindow = [mApplication keyWindow];
102: if ( keyWindow && [keyWindow respondsToSelector:@selector(autosaveWindowFrame)] )
103: [[keyWindow windowController] autosaveWindowFrame];
104:
105: // Now open the new window.
106: BrowserWindowController* controller = [self openBrowserWindowWithURLString:@"about:blank"];
107: [controller focusURLBar];
108: }
109:
110: -(IBAction)newTab:(id)aSender
111: {
112: NSWindow* keyWindow = [mApplication keyWindow];
113: if (keyWindow)
114: [[keyWindow windowController] newTab];
115: }
116:
117: -(IBAction)closeTab:(id)aSender
118: {
119: NSWindow* keyWindow = [mApplication keyWindow];
120: if (keyWindow)
121: [[keyWindow windowController] closeTab];
122: }
123:
124: -(IBAction) previousTab:(id)aSender
125: {
126: NSWindow* keyWindow = [mApplication keyWindow];
127: if (keyWindow)
128: [[keyWindow windowController] previousTab];
129: }
130:
131: -(IBAction) nextTab:(id)aSender;
132: {
133: NSWindow* keyWindow = [mApplication keyWindow];
134: if (keyWindow)
135: [[keyWindow windowController] nextTab];
136: }
137:
138: -(IBAction) openFile:(id)aSender
139: {
140: NSOpenPanel* openPanel = [[NSOpenPanel alloc] init];
141: [openPanel setCanChooseFiles: YES];
142: [openPanel setCanChooseDirectories: NO];
143: [openPanel setAllowsMultipleSelection: NO];
144: NSArray* array = [NSArray arrayWithObjects: @"htm",@"html",@"shtml",@"xhtml",@"xml",
145: @"txt",@"text",
146: @"gif",@"jpg",@"jpeg",@"png",@"bmp",
147: nil];
148: int result = [openPanel runModalForTypes: array];
149: if (result == NSOKButton) {
150: NSArray* urlArray = [openPanel URLs];
151: if ([urlArray count] == 0)
152: return;
153: NSURL* url = [urlArray objectAtIndex: 0];
154: // ----------------------
155: [[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:url];
156: // ----------------------
157: NSWindow* keyWindow = [mApplication keyWindow];
158: if (keyWindow)
159: return [[keyWindow windowController] loadURL: url];
160: else
161: [self openBrowserWindowWithURL: url];
162: }
163: }
164:
165: -(IBAction) openLocation:(id)aSender
166: {
167: NSWindow* keyWindow = [mApplication keyWindow];
168: if (!keyWindow) {
169: [self openBrowserWindowWithURLString: @"about:blank"];
170: keyWindow = [mApplication keyWindow];
171: }
172:
173: [[keyWindow windowController] performAppropriateLocationAction];
174: }
175:
176: -(IBAction) savePage:(id)aSender
177: {
178: NSWindow* keyWindow = [mApplication keyWindow];
179: if (keyWindow)
180: [[keyWindow windowController] saveDocument: mFilterView filterList: mFilterList];
181: }
182:
183: -(IBAction) printPage:(id)aSender
184: {
185: NSWindow* keyWindow = [mApplication keyWindow];
186: if (keyWindow)
187: [[keyWindow windowController] printDocument];
188: }
189:
190: -(IBAction) printPreview:(id)aSender
191: {
192: NSWindow* keyWindow = [mApplication keyWindow];
193: if (keyWindow)
194: [[keyWindow windowController] printPreview];
195: }
196:
197: -(IBAction) toggleOfflineMode:(id)aSender
198: {
199: nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
200: if (!ioService)
201: return;
202: PRBool offline = PR_FALSE;
203: ioService->GetOffline(&offline);
204: ioService->SetOffline(!offline);
205: mOffline = !offline;
206:
207: // Update the menu item text.
208: // Set the menu item's text to "Go Online" if we're currently
209: // offline.
210: if (mOffline)
211: [mOfflineMenuItem setTitle: @"Go Online"];
212: else
213: [mOfflineMenuItem setTitle: @"Work Offline"];
214:
215: // Indicate that we are working offline.
216: [[NSNotificationCenter defaultCenter] postNotificationName:@"offlineModeChanged" object:nil];
217: }
218:
219: // Edit menu actions.
220:
221:
222: //
223: // -findInPage
224: //
225: // Called in response to "Find" in edit menu. Opens the find dialog. We only keep
226: // one around for the whole app to use, showing/hiding as we see fit.
227: //
228: -(IBAction) findInPage:(id)aSender
229: {
230: if ( !mFindDialog )
231: mFindDialog = [[FindDlgController alloc] initWithWindowNibName: @"FindDialog"];
232: [mFindDialog showWindow:self];
233: }
234:
235: -(IBAction) findAgain:(id)aSender
236: {
237: NSWindow* keyWindow = [mApplication keyWindow];
238: if (keyWindow)
239: [[keyWindow windowController] findAgain];
240: }
241:
242: -(IBAction) goBack:(id)aSender
243: {
244: [[[mApplication keyWindow] windowController] back: aSender];
245: }
246:
247: -(IBAction) goForward:(id)aSender
248: {
249: [[[mApplication keyWindow] windowController] forward: aSender];
250: }
251:
252: -(IBAction) doReload:(id)aSender
253: {
254: [(BrowserWindowController*)([[mApplication keyWindow] windowController]) reload: aSender];
255: }
256:
257: -(IBAction) doStop:(id)aSender
258: {
259: [(BrowserWindowController*)([[mApplication keyWindow] windowController]) stop: aSender];
260: }
261:
262: -(IBAction) goHome:(id)aSender
263: {
264: [[[mApplication keyWindow] windowController] home: aSender];
265: }
266:
267: -(BrowserWindowController*)openBrowserWindowWithURLString: (NSString*)aURL
268: {
269: return [self openBrowserWindowWithURL: [NSURL URLWithString:aURL]];
270: }
271:
272: -(BrowserWindowController*)openBrowserWindowWithURL: (NSURL*)aURL
273: {
274: BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
275: [browser loadURL: aURL];
276: [browser showWindow: self];
277: return browser;
278: }
279:
280: -(void)applicationWillTerminate: (NSNotification*)aNotification
281: {
282: printf("Termination notification.\n");
283:
284: // Autosave one of the windows.
285: NSWindow* keyWindow = [mApplication keyWindow];
286: if (keyWindow)
287: [[keyWindow windowController] autosaveWindowFrame];
288:
289: mMenuBookmarks->RemoveObserver();
290: delete mMenuBookmarks;
291: mMenuBookmarks = nsnull;
292: nsCocoaBrowserService::TermEmbedding();
293: }
294:
295: // Bookmarks menu actions.
296: -(IBAction) addBookmark:(id)aSender
297: {
298: NSWindow* keyWindow = [mApplication keyWindow];
299: if (keyWindow)
300: [[keyWindow windowController] addBookmark];
301: }
302:
303: -(IBAction) openMenuBookmark:(id)aSender
304: {
305: NSWindow* keyWindow = [mApplication keyWindow];
306: if (!keyWindow) {
307: [self openBrowserWindowWithURLString: @"about:blank"];
308: keyWindow = [mApplication keyWindow];
309: }
310:
311: BookmarksService::OpenMenuBookmark([keyWindow windowController], aSender);
312: }
313:
314: - (MVPreferencesController *)preferencesController
315: {
316: if (!preferencesController) {
317: preferencesController = [[MVPreferencesController sharedInstance] retain];
318: }
319: return preferencesController;
320: }
321:
322: - (void)displayPreferencesWindow:sender
323: {
324: [[self preferencesController] showPreferences:nil] ;
325: }
326:
327: - (IBAction)showAboutBox:(id)sender
328: {
329: [[AboutBox sharedInstance] showPanel:sender];
330: }
331:
332: - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
333: {
334: NSWindow* keyWindow = [mApplication keyWindow];
335:
336: if (keyWindow) {
337: [[keyWindow windowController] loadURL:[NSURL fileURLWithPath:filename]];
338: } else {
339: [self openBrowserWindowWithURL:[NSURL fileURLWithPath:filename]];
340: }
341:
342: return YES;
343:
344: }
345:
346:
347: @end
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>