OSDN Git Service

import source-tree based svn r84.
[bluegriffon/BlueGriffon.git] / base / content / bluegriffon / js / recentPages.js
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is BlueGriffon.
15  *
16  * The Initial Developer of the Original Code is
17  * Disruptive Innovations SARL.
18  * Portions created by the Initial Developer are Copyright (C) 2006
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  *   Daniel Glazman <daniel.glazman@disruptive-innovations.com>, Original author
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 MPL, 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 MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 var RecentPagesHandler = {
39
40   appendRecentMenuitem: function(menupopup, title, url, menuIndex)
41   {
42     if (menupopup)
43     {
44       var menuItem = document.createElement("menuitem");
45       if (menuItem)
46       {
47         var accessKey;
48         if (menuIndex <= 9)
49           accessKey = String(menuIndex);
50         else if (menuIndex == 10)
51           accessKey = "0";
52         else
53           accessKey = " ";
54   
55         var itemString = accessKey+" ";
56   
57         // Show "title [url]" or just the URL
58         if (title)
59         {
60          itemString += title;
61          itemString += " [";
62         }
63         itemString += url;
64         if (title)
65           itemString += "]";
66   
67         menuItem.setAttribute("label", itemString);
68         menuItem.setAttribute("crop", "center");
69         menuItem.setAttribute("value", url);
70         if (accessKey != " ")
71           menuItem.setAttribute("accesskey", accessKey);
72         menupopup.appendChild(menuItem);
73       }
74     }
75   },
76   
77   buildRecentPagesMenu: function()
78   {
79     var editor = EditorUtils.getCurrentEditor();
80     if (!GetPrefs())
81       return;
82   
83     var popup = gDialog["menupopup_RecentFiles"];
84     if (!popup)
85       return;
86   
87     // Delete existing menu
88     while (popup.firstChild)
89       popup.removeChild(popup.firstChild);
90   
91     // Current page is the "0" item in the list we save in prefs,
92     //  but we don't include it in the menu.
93     var curUrl = "";
94     if (editor)
95       curUrl = UrlUtils.stripPassword(EditorUtils.getDocumentUrl());
96     var historyCount = 10;
97     try {
98       historyCount = GetPrefs().getIntPref("bluegriffon.history.url_maximum");
99     } catch(e) {}
100     var menuIndex = 1;
101   
102     for (var i = 0; i < historyCount; i++)
103     {
104       var url = GetUnicharPref("bluegriffon.history_url_"+i);
105   
106       // Skip over current url
107       if (url && url != curUrl)
108       {
109         // Build the menu
110         var title = GetUnicharPref("bluegriffon.history_title_"+i);
111         this.appendRecentMenuitem(popup, title, url, menuIndex);
112         menuIndex++;
113       }
114     }
115   },
116   
117   saveRecentFilesPrefs: function()
118   {
119     // Can't do anything if no prefs
120     if (!GetPrefs())
121       return;
122   
123     var curUrl = UrlUtils.stripPassword(EditorUtils.getDocumentUrl());
124     if (!curUrl)
125       return;
126     var historyCount = 10;
127     try {
128       historyCount = GetPrefs().getIntPref("bluegriffon.history.url_maximum"); 
129     } catch(e) {}
130   
131     var titleArray = [];
132     var urlArray = [];
133   
134     // XXX code below is suspect...
135     if (historyCount &&
136         !UrlUtils.isUrlAboutBlank(curUrl) &&
137         UrlUtils.getScheme(curUrl) != "data")
138     {
139       titleArray.push(EditorUtils.getDocumentTitle());
140       urlArray.push(curUrl);
141     }
142   
143     for (var i = 0; i < historyCount && urlArray.length < historyCount; i++)
144     {
145       var url = GetUnicharPref("bluegriffon.history_url_"+i);
146   
147       // Continue if URL pref is missing because 
148       //  a URL not found during loading may have been removed
149   
150       // Skip over current an "data" URLs
151       if (url && url != curUrl && UrlUtils.getScheme(url) != "data")
152       {
153         var title = GetUnicharPref("bluegriffon.history_title_"+i);
154         titleArray.push(title);
155         urlArray.push(url);
156       }
157     }
158   
159     // Resave the list back to prefs in the new order
160     for (i = 0; i < urlArray.length; i++)
161     {
162       SetUnicharPref("bluegriffon.history_title_"+i, titleArray[i]);
163       SetUnicharPref("bluegriffon.history_url_"+i, urlArray[i]);
164     }
165   }
166 };