view: update quote tree for implement convert method
[abstract.git] / view / aaSessionHistory.cpp
blob3bb507acf23d6eb06bc8eaa18d2c6336812944b9
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent tw=79 ft=cpp: */
3 /*
4 * Copyright (C) 2007,2008 Sergey Yanovich <ynvich@gmail.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
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 GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 #include "xpcom-config.h"
24 #include "nsCOMPtr.h"
25 #include "nsAutoPtr.h"
26 #include "nsIIOService.h"
27 #include "nsISHistory.h"
28 #include "nsIURI.h"
29 #include "nsIComponentManager.h"
30 #include "nsComponentManagerUtils.h"
31 #include "nsMemory.h"
32 #include "nsStringAPI.h"
33 #include "nsIClassInfoImpl.h"
34 #include "nsIServiceManager.h"
36 /* Unfrozen API */
37 #include "nsIDocShell.h"
38 #include "nsISHistoryInternal.h"
39 #include "nsITreeView.h"
40 #include "nsIWebNavigation.h"
42 /* Project includes */
43 #include "aaIPageView.h"
44 #include "aaLoadGroup.h"
45 #include "aaSessionHistory.h"
47 aaSessionHistory::aaSessionHistory()
48 :mChoosing(PR_FALSE)
50 mSH = do_CreateInstance(NS_SHISTORY_CONTRACTID);
51 if (!mSH)
52 return;
53 mSHI = do_QueryInterface(mSH);
54 if (!mSHI)
55 return;
56 mWN = do_QueryInterface(mSH);
57 if (!mWN)
58 return;
61 aaSessionHistory::~aaSessionHistory()
65 NS_IMPL_ISUPPORTS5_CI(aaSessionHistory,
66 nsISHistory,
67 nsISHistoryInternal,
68 nsIWebNavigation,
69 aaISHistory,
70 aaISession)
72 /* nsISHistoryInternal */
73 NS_IMETHODIMP
74 aaSessionHistory::AddEntry(nsISHEntry *aEntry, PRBool aPersist)
76 nsCOMPtr<aaIPageView> pageView( do_CreateInstance(
77 "@aasii.org/view/page-view;1", aEntry));
78 pageView->SetChoosing(mChoosing);
79 if (mChoosing)
80 mChoosing = PR_FALSE;
81 return mSHI->AddEntry(pageView, aPersist);
84 NS_IMETHODIMP
85 aaSessionHistory::GetRootTransaction(nsISHTransaction * *aRootTransaction)
87 return mSHI->GetRootTransaction(aRootTransaction);
90 NS_IMETHODIMP
91 aaSessionHistory::GetRootDocShell(nsIDocShell * *aRootDocShell)
93 return mSHI->GetRootDocShell( aRootDocShell );
95 NS_IMETHODIMP
96 aaSessionHistory::SetRootDocShell(nsIDocShell * aRootDocShell)
98 return mSHI->SetRootDocShell( aRootDocShell );
101 NS_IMETHODIMP
102 aaSessionHistory::UpdateIndex()
104 return mSHI->UpdateIndex();
107 NS_IMETHODIMP
108 aaSessionHistory::ReplaceEntry(PRInt32 aIndex, nsISHEntry *aReplaceEntry)
110 if (!aReplaceEntry->GetIID().Equals(NS_GET_IID(aaIPageView))) {
111 nsCOMPtr<aaIPageView> pageView(do_CreateInstance("@aasii.org/view/page-view;1", aReplaceEntry));
112 pageView->SetChoosing(mChoosing);
113 return mSHI->ReplaceEntry(aIndex, pageView);
115 return mSHI->ReplaceEntry(aIndex, aReplaceEntry);
118 NS_IMETHODIMP
119 aaSessionHistory::GetListener(nsISHistoryListener * *aListener)
121 return mSHI->GetListener(aListener);
124 NS_IMETHODIMP
125 aaSessionHistory::EvictContentViewers(PRInt32 previousIndex, PRInt32 index)
127 return mSHI->EvictContentViewers(previousIndex, index);
130 NS_IMETHODIMP
131 aaSessionHistory::EvictExpiredContentViewerForEntry(nsISHEntry *aEntry)
133 return mSHI->EvictExpiredContentViewerForEntry(aEntry);
136 NS_IMETHODIMP
137 aaSessionHistory::EvictAllContentViewers()
139 return mSHI->EvictAllContentViewers();
142 /* nsIWebNavigation */
143 NS_IMETHODIMP
144 aaSessionHistory::LoadURI(const PRUnichar *aURI, PRUint32 aLoadFlags,
145 nsIURI *aReferrer, nsIInputStream *aPostData, nsIInputStream *aHeaders)
147 nsresult rv;
148 nsIDocShell *docShell;
149 rv = mSHI->GetRootDocShell(&docShell);
150 NS_ENSURE_SUCCESS(rv, rv);
152 nsRefPtr<aaLoadGroup> loader = new aaLoadGroup(docShell);
153 rv = loader->Init(aURI, aLoadFlags, aReferrer, aPostData, aHeaders);
154 NS_ENSURE_SUCCESS(rv, rv);
156 return NS_OK;
159 /* aaISHistory */
160 NS_IMETHODIMP
161 aaSessionHistory::GetChoosing(PRBool *aChoosing)
163 nsresult rv;
164 nsCOMPtr<aaIPageView> pv1 = getter_AddRefs( getDestination(0, rv) );
165 NS_ENSURE_TRUE(pv1, rv);
167 return pv1->GetChoosing(aChoosing);
170 NS_IMETHODIMP
171 aaSessionHistory::BeginQuery()
173 if ( mChoosing )
174 return NS_ERROR_ALREADY_INITIALIZED;
175 mChoosing = PR_TRUE;
176 return NS_OK;
179 NS_IMETHODIMP
180 aaSessionHistory::SetFile(nsIFile *file)
182 mFile = file;
183 if (!mFile)
184 mSession = nsnull;
186 return NS_OK;
189 NS_IMETHODIMP
190 aaSessionHistory::GetQueryBuffer(nsISupports * *aQueryBuffer)
192 nsresult rv;
193 nsCOMPtr<aaIPageView> pv1 = getter_AddRefs( getDestination(0, rv) );
194 NS_ENSURE_TRUE(pv1, rv);
196 return pv1->GetQueryChoice(aQueryBuffer);
199 NS_IMETHODIMP
200 aaSessionHistory::GetChoice(nsISupports * *aChoice)
202 nsresult rv;
203 nsCOMPtr<aaIPageView> pv0 = getter_AddRefs( getDestination(-1, rv) );
204 NS_ENSURE_TRUE(pv0, rv);
206 return pv0->GetQueryChoice(aChoice);
208 NS_IMETHODIMP
209 aaSessionHistory::SetChoice(nsISupports * aChoice)
211 nsresult rv;
212 nsCOMPtr<aaIPageView> pv0 = getter_AddRefs( getDestination(-1, rv) );
213 NS_ENSURE_TRUE(pv0, rv);
215 return pv0->SetQueryChoice(aChoice);
218 NS_IMETHODIMP
219 aaSessionHistory::CreateTreeView(const char *aContract, nsITreeView * *_retval)
221 NS_ENSURE_ARG_POINTER(_retval);
222 nsCOMPtr<nsIComponentManager> compMgr;
223 nsresult rv = NS_GetComponentManager(getter_AddRefs(compMgr));
224 if (compMgr)
225 rv = compMgr->CreateInstanceByContractID(aContract, get_mSession(),
226 NS_GET_IID(nsITreeView), (void **) _retval);
228 return rv;
231 /* Private methods */
232 aaIPageView *
233 aaSessionHistory::getDestination(PRInt32 offset, nsresult &rv)
235 PRInt32 index;
236 rv = mSH->GetIndex(&index);
237 NS_ENSURE_SUCCESS(rv, nsnull);
239 nsCOMPtr<nsIHistoryEntry> he0;
240 rv = mSH->GetEntryAtIndex(index + offset, PR_FALSE, getter_AddRefs( he0 ));
241 NS_ENSURE_SUCCESS(rv, nsnull);
243 aaIPageView *pv0;
244 rv = CallQueryInterface(he0, &pv0);
245 return pv0;
248 aaISession *
249 aaSessionHistory::get_mSession()
251 nsresult rv;
252 if (!mSession) {
253 mSession = do_CreateInstance("@aasii.org/storage/session;1", mFile, &rv);
254 NS_ENSURE_SUCCESS(rv, nsnull);
256 return (mSession);