Diff for /chimera/BookmarksService.mm between versions 1.7 and 1.17

version 1.7, 2002/02/09 23:17:02 version 1.17, 2002/03/10 08:14:36
Line 1 Line 1
/*/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 *  BookmarksService.cpp/* ***** BEGIN LICENSE BLOCK *****
 *  Chimera * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  *   *
 *  Created by David Hyatt on Thu Feb 07 2002. * The contents of this file are subject to the Netscape Public License
 *  Copyright (c) 2001 __MyCompanyName__. All rights reserved. * Version 1.1 (the "License"); you may not use this file except in
  * compliance with the License. You may obtain a copy of the License at
  * http://www.mozilla.org/NPL/
  *   *
 */ * Software distributed under the License is distributed on an "AS IS" basis,
  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  * for the specific language governing rights and limitations under the
  * License.
  * The Original Code is mozilla.org code.
  * The Initial Developer of the Original Code is 
  * Netscape Communications Corporation.
  * Portions created by the Initial Developer are Copyright (C) 2002
  * the Initial Developer. All Rights Reserved.
  * Contributor(s):
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the NPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the NPL, the GPL or the LGPL.
  * ***** END LICENSE BLOCK ***** */
 #import "NSBrowserView.h"  #import "NSBrowserView.h"
 #include "BookmarksService.h"  #include "BookmarksService.h"
Line 85 Line 113
                             getter_AddRefs(elt));                              getter_AddRefs(elt));
     // Fetch the title of the current page and the URL.      // Fetch the title of the current page and the URL.
    nsCOMPtr<nsIWebBrowser> webBrowser = getter_AddRefs([[mBrowserView getBrowserView] getWebBrowser]);    nsCOMPtr<nsIWebBrowser> webBrowser = getter_AddRefs([[[mBrowserWindowController getMyBrowserView] getBrowserView] getWebBrowser]);
     nsCOMPtr<nsIDOMWindow> window;      nsCOMPtr<nsIDOMWindow> window;
     webBrowser->GetContentDOMWindow(getter_AddRefs(window));      webBrowser->GetContentDOMWindow(getter_AddRefs(window));
     nsCOMPtr<nsIDOMDocument> htmlDoc;      nsCOMPtr<nsIDOMDocument> htmlDoc;
Line 96 Line 124
     if (pageDoc) {      if (pageDoc) {
         nsCOMPtr<nsIURI> url;          nsCOMPtr<nsIURI> url;
         pageDoc->GetDocumentURL(getter_AddRefs(url));          pageDoc->GetDocumentURL(getter_AddRefs(url));
        nsXPIDLCString spec;        nsCAutoString spec;
        url->GetSpec(getter_Copies(spec));        url->GetSpec(spec);
         href.AssignWithConversion(spec.get());          href.AssignWithConversion(spec.get());
     }      }
Line 167 Line 195
         if (!href.IsEmpty()) {          if (!href.IsEmpty()) {
             nsCAutoString cstr; cstr.AssignWithConversion(href);              nsCAutoString cstr; cstr.AssignWithConversion(href);
             NSString* url = [NSString stringWithCString: cstr.get()];              NSString* url = [NSString stringWithCString: cstr.get()];
            [[mBrowserView getBrowserView] loadURI:[NSURL URLWithString: url] flags:NSLoadFlagsNone];            [[[mBrowserWindowController getMyBrowserView] getBrowserView] loadURI:[NSURL URLWithString: url] flags:NSLoadFlagsNone];
         }          }
     }       } 
 }  }
   - (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
       return NO;
 - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item  - (id)outlineView:(NSOutlineView *)outlineView child:(int)index ofItem:(id)item
 {  {
     if (!mBookmarks)      if (!mBookmarks)
Line 222 Line 255
 - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item  - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
 {  {
    if (!item)    NSString                                    *columnName = [tableColumn identifier];
        return nil;    NSMutableAttributedString       *cellValue = [[NSMutableAttributedString alloc] init];
        NSFileWrapper                               *fileWrapper = [[NSFileWrapper alloc] initRegularFileWithContents:nil];
    NSString* columnName = [tableColumn identifier];    NSTextAttachment                    *textAttachment = [[NSTextAttachment alloc] initWithFileWrapper:fileWrapper];
          NSMutableAttributedString   *attachmentAttrString = nil;
     NSCell                                              *attachmentAttrStringCell;
     if ([columnName isEqualToString: @"name"]) {      if ([columnName isEqualToString: @"name"]) {
         nsIContent* content = [item contentNode];          nsIContent* content = [item contentNode];
         nsAutoString nameAttr;          nsAutoString nameAttr;
         content->GetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, nameAttr);          content->GetAttr(kNameSpaceID_None, BookmarksService::gNameAtom, nameAttr);
         nsCAutoString cStr; cStr.AssignWithConversion(nameAttr);          nsCAutoString cStr; cStr.AssignWithConversion(nameAttr);
        return [NSString stringWithCString: cStr.get()];        
         //Set cell's textual contents
         [cellValue replaceCharactersInRange:NSMakeRange(0, [cellValue length])
                                  withString:[NSString stringWithCString: cStr.get()]];
         //Create an attributed string to hold the empty attachment, then release the components.
         attachmentAttrString = [[NSMutableAttributedString attributedStringWithAttachment:textAttachment] retain];
         [textAttachment release];
         [fileWrapper release];
         //Get the cell of the text attachment.
         attachmentAttrStringCell = (NSCell *)[(NSTextAttachment *)[attachmentAttrString attribute:NSAttachmentAttributeName
                                                                                    effectiveRange:nil] attachmentCell];
         //Figure out which image to add, and set the cell's image.
         if ( [self outlineView:outlineView isItemExpandable:item] ) {
             [attachmentAttrStringCell setImage:[NSImage imageNamed:@"folder"]];
         } else {
             [attachmentAttrStringCell setImage:[NSImage imageNamed:@"smallbookmark"]];
         //Insert the image
         [cellValue replaceCharactersInRange:NSMakeRange(0, 0) withAttributedString:attachmentAttrString];
         //Tweak the baseline to vertically center the text.
         [cellValue addAttribute:NSBaselineOffsetAttributeName
                           value:[NSNumber numberWithFloat:-3.0]
                           range:NSMakeRange(0, 1)];
     }      }
        return cellValue;
    return nil; 
 }  }
 - (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item  - (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
Line 301  StripWhitespaceNodes(nsIContent* aElemen Line 361  StripWhitespaceNodes(nsIContent* aElemen
 PRUint32 BookmarksService::gRefCnt = 0;  PRUint32 BookmarksService::gRefCnt = 0;
 nsIDocument* BookmarksService::gBookmarks = nsnull;  nsIDocument* BookmarksService::gBookmarks = nsnull;
 NSMutableDictionary* BookmarksService::gDictionary = nil;  NSMutableDictionary* BookmarksService::gDictionary = nil;
NSMutableDictionary* BookmarksService::gBookmarkToMenuMap = nil;MainController* BookmarksService::gMainController = nil;
NSMutableDictionary* BookmarksService::gMenuToBookmarkMap = nil; 
 nsIAtom* BookmarksService::gFolderAtom = nsnull;  nsIAtom* BookmarksService::gFolderAtom = nsnull;
 nsIAtom* BookmarksService::gBookmarkAtom = nsnull;  nsIAtom* BookmarksService::gBookmarkAtom = nsnull;
 nsIAtom* BookmarksService::gHrefAtom = nsnull;  nsIAtom* BookmarksService::gHrefAtom = nsnull;
Line 394  BookmarksService::AddObserver() Line 453  BookmarksService::AddObserver()
         gNameAtom = NS_NewAtom("name");          gNameAtom = NS_NewAtom("name");
         gHrefAtom = NS_NewAtom("href");          gHrefAtom = NS_NewAtom("href");
         gInstances = new nsVoidArray();          gInstances = new nsVoidArray();
         nsCOMPtr<nsIFile> profileDir;          nsCOMPtr<nsIFile> profileDir;
         NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDir));          NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(profileDir));
         profileDir->Append("bookmarks.xml");          profileDir->Append("bookmarks.xml");
        nsXPIDLCString bookmarksFileURL;        nsCAutoString bookmarksFileURL;
        NS_GetURLSpecFromFile(profileDir, getter_Copies(bookmarksFileURL));        NS_GetURLSpecFromFile(profileDir, bookmarksFileURL);
         nsCOMPtr<nsIURI> uri;          nsCOMPtr<nsIURI> uri;
         NS_NewURI(getter_AddRefs(uri), bookmarksFileURL.get());          NS_NewURI(getter_AddRefs(uri), bookmarksFileURL.get());
Line 432  BookmarksService::RemoveObserver() Line 491  BookmarksService::RemoveObserver()
         NS_RELEASE(gNameAtom);          NS_RELEASE(gNameAtom);
         NS_RELEASE(gHrefAtom);          NS_RELEASE(gHrefAtom);
         [gDictionary release];          [gDictionary release];
         [gBookmarkToMenuMap release];  
         [gMenuToBookmarkMap release];  
     }      }
 }  }
Line 456  BookmarksService::FlushBookmarks() Line 513  BookmarksService::FlushBookmarks()
 void  void
 BookmarksService::ConstructBookmarksMenu(NSMenu* aMenu, nsIContent* aContent)  BookmarksService::ConstructBookmarksMenu(NSMenu* aMenu, nsIContent* aContent)
 {  {
     // Create our menu maps.  
     if (!gMenuToBookmarkMap)  
         gMenuToBookmarkMap = [[NSMutableDictionary alloc] initWithCapacity: 30];  
     if (!gBookmarkToMenuMap)  
         gBookmarkToMenuMap = [[NSMutableDictionary alloc] initWithCapacity: 30];  
     nsCOMPtr<nsIContent> content = aContent;      nsCOMPtr<nsIContent> content = aContent;
     if (!content) {      if (!content) {
         GetRootContent(getter_AddRefs(content));          GetRootContent(getter_AddRefs(content));
        // Add the root entry to our maps.        GetWrapperFor(content);
        BookmarkItem* item = GetWrapperFor(content); 
        [gMenuToBookmarkMap setObject: item forKey: aMenu]; 
        [gBookmarkToMenuMap setObject: aMenu forKey: item]; 
     }      }
     // Now walk our children, and for folders also recur into them.      // Now walk our children, and for folders also recur into them.
Line 486  BookmarksService::ConstructBookmarksMenu Line 534  BookmarksService::ConstructBookmarksMenu
         NSString* title = [NSString stringWithCString: nameCStr.get()];          NSString* title = [NSString stringWithCString: nameCStr.get()];
         // Create a menu or menu item for the child.          // Create a menu or menu item for the child.
           NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle: title action: NULL keyEquivalent: @""] autorelease];
           [aMenu addItem: menuItem];
         nsCOMPtr<nsIAtom> tag;          nsCOMPtr<nsIAtom> tag;
        content->GetTag(*getter_AddRefs(tag));        child->GetTag(*getter_AddRefs(tag));
         if (tag == gFolderAtom) {          if (tag == gFolderAtom) {
             // We are building a menu.  
             NSMenu* menu = [[[NSMenu alloc] initWithTitle: title] autorelease];              NSMenu* menu = [[[NSMenu alloc] initWithTitle: title] autorelease];
            BookmarkItem* item = GetWrapperFor(child);            [aMenu setSubmenu: menu forItem: menuItem];
            [gMenuToBookmarkMap setObject: item forKey: menu];            [menu setAutoenablesItems: NO];
            [gBookmarkToMenuMap setObject: menu forKey: item]; 
            [aMenu addItem: menu]; 
             ConstructBookmarksMenu(menu, child);              ConstructBookmarksMenu(menu, child);
         }          }
         else {          else {
            // We are building a menu item.            [menuItem setTarget: gMainController];
            NSMenuItem* menuItem = [[[NSMenuItem alloc] initWithTitle: title action: NULL keyEquivalent: @""] autorelease];            [menuItem setAction: @selector(openMenuBookmark:)];
            BookmarkItem* item = GetWrapperFor(child); 
            [gMenuToBookmarkMap setObject: item forKey: menuItem]; 
            [gBookmarkToMenuMap setObject: menuItem forKey: item]; 
            [aMenu addItem: menuItem]; 
         }          }
           PRUint32 contentID;
           [menuItem setTag: contentID];
     }      }
 }  }
   BookmarksService::OpenMenuBookmark(BrowserWindowController* aController, id aMenuItem)
       // Get the corresponding bookmark item.
       BookmarkItem* item = [gDictionary objectForKey: [NSNumber numberWithInt: [aMenuItem tag]]];
       // Get the content node.
       nsIContent* content = [item contentNode];
       // Get the href attribute.  This is the URL we want to load.
       nsAutoString href;
       content->GetAttr(kNameSpaceID_None, gHrefAtom, href);
       nsCAutoString cref; cref.AssignWithConversion(href);
       if (cref.IsEmpty())
       NSString* url = [NSString stringWithCString: cref.get()];
       // Now load the URL in the window.
       [aController loadURL:[NSURL URLWithString: url]];

Removed from v.1.7  
changed lines
  Added in v.1.17

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>