Initial import of ephy (rev# 7126) from svn
[ephy-soc.git] / embed / mozilla / .svn / text-base / GlobalHistory.cpp.svn-base
blobefcda79f96ec6aa8aaedeedfc05627473a092e4f
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  *  Copyright © 2001, 2004 Philip Langdale
4  *  Copyright © 2004 Christian Persch
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  *  $Id$
21  */
23 #include "mozilla-config.h"
24 #include "config.h"
26 #include <nsStringAPI.h>
28 #include <nsIURI.h>
30 #include "ephy-embed-shell.h"
32 #include "GlobalHistory.h"
35 #define MAX_TITLE_LENGTH        2048
36 #define MAX_URL_LENGTH          16384
38 #ifdef HAVE_NSIGLOBALHISTORY3_H
39 NS_IMPL_ISUPPORTS2 (MozGlobalHistory, nsIGlobalHistory2, nsIGlobalHistory3)
40 #else
41 NS_IMPL_ISUPPORTS1 (MozGlobalHistory, nsIGlobalHistory2)
42 #endif /* HAVE_NSIGLOBALHISTORY3_H */
44 MozGlobalHistory::MozGlobalHistory ()
46         mGlobalHistory = EPHY_HISTORY (ephy_embed_shell_get_global_history (embed_shell));
48         mHistoryListener = new EphyHistoryListener ();
49         mHistoryListener->Init (mGlobalHistory);
52 MozGlobalHistory::~MozGlobalHistory ()
56 /* void addURI (in nsIURI aURI, in boolean aRedirect, in boolean aToplevel, in nsIURI aReferrer); */
57 NS_IMETHODIMP MozGlobalHistory::AddURI(nsIURI *aURI,
58                                        PRBool aRedirect,
59                                        PRBool aToplevel,
60                                        nsIURI *aReferrer)
62         nsresult rv;
64         NS_ENSURE_ARG (aURI);
66         // filter out unwanted URIs such as chrome: etc
67         // The model is really if we don't know differently then add which basically
68         // means we are suppose to try all the things we know not to allow in and
69         // then if we don't bail go on and allow it in.  But here lets compare
70         // against the most common case we know to allow in and go on and say yes
71         // to it.
73         PRBool isHTTP = PR_FALSE, isHTTPS = PR_FALSE;
74         rv = aURI->SchemeIs("http", &isHTTP);
75         rv |= aURI->SchemeIs("https", &isHTTPS);
76         NS_ENSURE_SUCCESS (rv, NS_ERROR_FAILURE);
78         if (!isHTTP && !isHTTPS)
79         {
80                 static const char *schemes[] = { "javascript",
81                                                  "data",
82                                                  "about",
83                                                  "chrome",
84                                                  "resource",
85                                                  "view-source" };
87                 for (PRUint32 i = 0; i < G_N_ELEMENTS (schemes); ++i)
88                 {
89                         PRBool result = PR_FALSE;
90                         if (NS_SUCCEEDED (aURI->SchemeIs (schemes[i], &result)) && result)
91                         {
92                                 return NS_OK;
93                         }
94                 }
95         }
97         nsCString spec;
98         rv = aURI->GetSpec(spec);
99         NS_ENSURE_TRUE (NS_SUCCEEDED(rv) && spec.Length(), rv);
101         if (spec.Length () > MAX_URL_LENGTH) return NS_OK;
103         ephy_history_add_page (mGlobalHistory, spec.get(), aRedirect, aToplevel);
104         
105         return NS_OK;
108 /* boolean isVisited (in nsIURI aURI); */
109 NS_IMETHODIMP MozGlobalHistory::IsVisited(nsIURI *aURI,
110                                           PRBool *_retval)
112         NS_ENSURE_ARG (aURI);
114         *_retval = PR_FALSE;
116         nsCString spec;
117         aURI->GetSpec(spec);
119         if (spec.Length () > MAX_URL_LENGTH) return NS_OK;
121         *_retval = ephy_history_is_page_visited (mGlobalHistory, spec.get());
122         
123         return NS_OK;
126 /* void setPageTitle (in nsIURI aURI, in AString aTitle); */
127 NS_IMETHODIMP MozGlobalHistory::SetPageTitle(nsIURI *aURI,
128                                              const nsAString & aTitle)
130         NS_ENSURE_ARG (aURI);
132         nsCString spec;
133         aURI->GetSpec(spec);
135         if (spec.Length () > MAX_URL_LENGTH) return NS_OK;
137         nsString uTitle (aTitle);
139         /* This depends on the assumption that 
140          * typeof(PRUnichar) == typeof (gunichar2) == uint16,
141          * which should be pretty safe.
142          */
143         glong n_read = 0, n_written = 0;
144         char *converted = g_utf16_to_utf8 ((gunichar2*) uTitle.get(), MAX_TITLE_LENGTH,
145                                             &n_read, &n_written, NULL);
146         /* FIXME loop from the end while !g_unichar_isspace (char)? */
147         if (converted == NULL) return NS_OK;
149         ephy_history_set_page_title (mGlobalHistory, spec.get(), converted);
151         g_free (converted);
153         return NS_OK;
156 #ifdef HAVE_NSIGLOBALHISTORY3_H
158 #ifdef HAVE_GECKO_1_9
160 /* unsigned long getURIGeckoFlags(in nsIURI aURI); */
161 NS_IMETHODIMP
162 MozGlobalHistory::GetURIGeckoFlags(nsIURI *aURI,
163                                    PRUint32* aFlags)
165         *aFlags = 0;
167         nsCString spec;
168         aURI->GetSpec(spec);
170         if (spec.Length () > MAX_URL_LENGTH) return NS_OK;
172         EphyNode *page = ephy_history_get_page (mGlobalHistory, spec.get());
174         GValue value = { 0, };
175         if (page != NULL &&
176             ephy_node_get_property (page, EPHY_NODE_PAGE_PROP_GECKO_FLAGS, &value))
177         {
178                 *aFlags = (PRUint32) (gulong) g_value_get_long (&value);
179                 g_value_unset (&value);
181                 return NS_OK;
182         }
184         return NS_ERROR_FAILURE;
187 /* void setURIGeckoFlags(in nsIURI aURI, in unsigned long aFlags); */
188 NS_IMETHODIMP
189 MozGlobalHistory::SetURIGeckoFlags(nsIURI *aURI,
190                                    PRUint32 aFlags)
192         nsCString spec;
193         aURI->GetSpec(spec);
195         if (spec.Length () > MAX_URL_LENGTH) return NS_OK;
197         EphyNode *page = ephy_history_get_page (mGlobalHistory, spec.get());
198         if (page != NULL)
199         {
200                 ephy_node_set_property_long (page,
201                                              EPHY_NODE_PAGE_PROP_GECKO_FLAGS,
202                                              aFlags);
203                 return NS_OK;
204         }
206         return NS_ERROR_FAILURE;
209 #endif /* HAVE_GECKO_1_9 */
211 /* void addDocumentRedirect (in nsIChannel 
212                              aOldChannel, 
213                              in nsIChannel aNewChannel, 
214                              in PRInt32 aFlags, 
215                              in boolean aTopLevel); */
216 NS_IMETHODIMP 
217 MozGlobalHistory::AddDocumentRedirect(nsIChannel *aOldChannel, 
218                                       nsIChannel *aNewChannel, 
219                                       PRInt32 aFlags, 
220                                       PRBool aTopLevel)
222     return NS_ERROR_NOT_IMPLEMENTED;
225 #endif /* HAVE_NSIGLOBALHISTORY3_H */