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:
46: static const char* ioServiceContractID = "@mozilla.org/network/io-service;1";
47:
48: @implementation MainController
49:
50: -(void)dealloc
51: {
52: [super dealloc];
53: printf("Main controller died.\n");
54: }
55:
56: -(void)awakeFromNib
57: {
58: [self newWindow: self];
59:
60: [mBookmarksMenu setAutoenablesItems: NO];
61:
62: mMenuBookmarks = new BookmarksService(nil);
63: mMenuBookmarks->AddObserver();
64: mMenuBookmarks->ConstructBookmarksMenu(mBookmarksMenu, nsnull);
65: BookmarksService::gMainController = self;
66:
67: // Initialize offline mode.
68: mOffline = NO;
69: nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
70: if (!ioService)
71: return;
72: PRBool offline = PR_FALSE;
73: ioService->GetOffline(&offline);
74: mOffline = offline;
75:
76: // Set the menu item's text to "Go Online" if we're currently
77: // offline.
78: if (mOffline)
79: [mOfflineMenuItem setTitle: @"Go Online"];
80: }
81:
82: -(IBAction)newWindow:(id)aSender
83: {
84: BrowserWindowController* controller = [self openBrowserWindowWithURLString:@"about:blank"];
85: [controller focusURLBar];
86: }
87:
88: -(IBAction)newTab:(id)aSender
89: {
90: NSWindow* keyWindow = [mApplication keyWindow];
91: if (keyWindow)
92: [[keyWindow windowController] newTab];
93: }
94:
95: -(IBAction)closeTab:(id)aSender
96: {
97: NSWindow* keyWindow = [mApplication keyWindow];
98: if (keyWindow)
99: [[keyWindow windowController] closeTab];
100: }
101:
102: -(IBAction) previousTab:(id)aSender
103: {
104: NSWindow* keyWindow = [mApplication keyWindow];
105: if (keyWindow)
106: [[keyWindow windowController] previousTab];
107: }
108:
109: -(IBAction) nextTab:(id)aSender;
110: {
111: NSWindow* keyWindow = [mApplication keyWindow];
112: if (keyWindow)
113: [[keyWindow windowController] nextTab];
114: }
115:
116: -(IBAction) openFile:(id)aSender
117: {
118: NSOpenPanel* openPanel = [[NSOpenPanel alloc] init];
119: [openPanel setCanChooseFiles: YES];
120: [openPanel setCanChooseDirectories: NO];
121: [openPanel setAllowsMultipleSelection: NO];
122: NSArray* array = [NSArray arrayWithObjects: @"htm",@"html",@"shtml",@"xhtml",@"xml",
123: @"txt",@"text",
124: @"gif",@"jpg",@"jpeg",@"png",@"bmp",
125: nil];
126: int result = [openPanel runModalForTypes: array];
127: if (result == NSOKButton) {
128: NSArray* urlArray = [openPanel URLs];
129: if ([urlArray count] == 0)
130: return;
131: NSURL* url = [urlArray objectAtIndex: 0];
132: NSWindow* keyWindow = [mApplication keyWindow];
133: if (keyWindow)
134: return [[keyWindow windowController] loadURL: url];
135: else
136: [self openBrowserWindowWithURL: url];
137: }
138: }
139:
140: -(IBAction) openLocation:(id)aSender
141: {
142: NSWindow* keyWindow = [mApplication keyWindow];
143: if (!keyWindow) {
144: [self openBrowserWindowWithURLString: @"about:blank"];
145: keyWindow = [mApplication keyWindow];
146: }
147:
148: [[keyWindow windowController] performAppropriateLocationAction];
149: }
150:
151: -(IBAction) savePage:(id)aSender
152: {
153: NSWindow* keyWindow = [mApplication keyWindow];
154: if (keyWindow)
155: [[keyWindow windowController] saveDocument: mFilterView filterList: mFilterList];
156: }
157:
158: -(IBAction) printPage:(id)aSender
159: {
160: NSWindow* keyWindow = [mApplication keyWindow];
161: if (keyWindow)
162: [[keyWindow windowController] printDocument];
163: }
164:
165: -(IBAction) printPreview:(id)aSender
166: {
167: NSWindow* keyWindow = [mApplication keyWindow];
168: if (keyWindow)
169: [[keyWindow windowController] printPreview];
170: }
171:
172: -(IBAction) toggleOfflineMode:(id)aSender
173: {
174: nsCOMPtr<nsIIOService> ioService(do_GetService(ioServiceContractID));
175: if (!ioService)
176: return;
177: PRBool offline = PR_FALSE;
178: ioService->GetOffline(&offline);
179: ioService->SetOffline(!offline);
180: mOffline = !offline;
181:
182: // Update the menu item text.
183: // Set the menu item's text to "Go Online" if we're currently
184: // offline.
185: if (mOffline)
186: [mOfflineMenuItem setTitle: @"Go Online"];
187: else
188: [mOfflineMenuItem setTitle: @"Work Offline"];
189:
190: // Indicate that we are working offline.
191: [[NSNotificationCenter defaultCenter] postNotificationName:@"offlineModeChanged" object:nil];
192: }
193:
194: // Edit menu actions.
195: -(IBAction) findInPage:(id)aSender
196: {
197: NSWindow* keyWindow = [mApplication keyWindow];
198: if (keyWindow)
199: [[keyWindow windowController] findInPage];
200: }
201:
202: -(IBAction) findAgain:(id)aSender
203: {
204: NSWindow* keyWindow = [mApplication keyWindow];
205: if (keyWindow)
206: [[keyWindow windowController] findAgain];
207: }
208:
209: -(IBAction) goBack:(id)aSender
210: {
211: [[[mApplication keyWindow] windowController] back: aSender];
212: }
213:
214: -(IBAction) goForward:(id)aSender
215: {
216: [[[mApplication keyWindow] windowController] forward: aSender];
217: }
218:
219: -(IBAction) doReload:(id)aSender
220: {
221: [(BrowserWindowController*)([[mApplication keyWindow] windowController]) reload: aSender];
222: }
223:
224: -(IBAction) doStop:(id)aSender
225: {
226: [(BrowserWindowController*)([[mApplication keyWindow] windowController]) stop: aSender];
227: }
228:
229: -(IBAction) goHome:(id)aSender
230: {
231: [[[mApplication keyWindow] windowController] home: aSender];
232: }
233:
234: -(BrowserWindowController*)openBrowserWindowWithURLString: (NSString*)aURL
235: {
236: return [self openBrowserWindowWithURL: [NSURL URLWithString:aURL]];
237: }
238:
239: -(BrowserWindowController*)openBrowserWindowWithURL: (NSURL*)aURL
240: {
241: BrowserWindowController* browser = [[BrowserWindowController alloc] initWithWindowNibName: @"BrowserWindow"];
242: [browser loadURL: aURL];
243: [browser showWindow: self];
244: return browser;
245: }
246:
247: -(void)applicationWillTerminate: (NSNotification*)aNotification
248: {
249: printf("Termination notification.\n");
250: mMenuBookmarks->RemoveObserver();
251: delete mMenuBookmarks;
252: mMenuBookmarks = nsnull;
253: nsCocoaBrowserService::TermEmbedding();
254: }
255:
256: // Bookmarks menu actions.
257: -(IBAction) addBookmark:(id)aSender
258: {
259: NSWindow* keyWindow = [mApplication keyWindow];
260: if (keyWindow)
261: [[keyWindow windowController] addBookmark];
262: }
263:
264: -(IBAction) openMenuBookmark:(id)aSender
265: {
266: NSWindow* keyWindow = [mApplication keyWindow];
267: if (!keyWindow) {
268: [self openBrowserWindowWithURLString: @"about:blank"];
269: keyWindow = [mApplication keyWindow];
270: }
271:
272: BookmarksService::OpenMenuBookmark([keyWindow windowController], aSender);
273: }
274:
275: - (MVPreferencesController *)preferencesController
276: {
277: if (!preferencesController) {
278: preferencesController = [[MVPreferencesController sharedInstance] retain];
279: }
280: return preferencesController;
281: }
282:
283: - (void)displayPreferencesWindow:sender
284: {
285: [[self preferencesController] showPreferences:nil] ;
286: }
287:
288:
289: @end
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>