1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: Mozilla-sample-code 1.0
5
* Copyright (c) 2002 Netscape Communications Corporation and
8
* Permission is hereby granted, free of charge, to any person obtaining a
9
* copy of this Mozilla sample software and associated documentation files
10
* (the "Software"), to deal in the Software without restriction, including
11
* without limitation the rights to use, copy, modify, merge, publish,
12
* distribute, sublicense, and/or sell copies of the Software, and to permit
13
* persons to whom the Software is furnished to do so, subject to the
14
* following conditions:
16
* The above copyright notice and this permission notice shall be included
17
* in all copies or substantial portions of the Software.
19
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25
* DEALINGS IN THE SOFTWARE.
28
* Rod Spears <rods@netscape.com>
30
* ***** END LICENSE BLOCK ***** */
33
// CMostRecentUrls object is responsible for keeping track of the
34
// 16 most recently used URLs. It stores this list in a file named
35
// "urls.txt" on a per profile basis in the user's profile directory
37
// The constructor loads the URL list
38
// The destructor saves the URL list
43
#include "nsILocalFile.h"
44
#include "nsAppDirectoryServiceDefs.h"
45
#include "MostRecentUrls.h"
47
//--------------------------------------------------------
49
//--------------------------------------------------------
50
CMostRecentUrls::CMostRecentUrls() :
53
for (int i=0;i<MAX_URLS;i++) {
57
FILE * fd = GetFD("r");
60
while (fgets(line, 512, fd)) {
61
if (strlen(line) > 1) {
62
line[strlen(line)-1] = 0;
63
mURLs[mNumURLs++] = _strdup(line);
71
FILE * CMostRecentUrls::GetFD(const char * aMode)
74
nsCOMPtr<nsIFile> file;
75
nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(file));
76
if (NS_SUCCEEDED(rv)) {
77
nsCOMPtr<nsILocalFile> local_file(do_QueryInterface(file));
78
local_file->AppendNative(NS_LITERAL_CSTRING("urls.txt"));
79
local_file->OpenANSIFileDesc(aMode, &fd);
85
CMostRecentUrls::~CMostRecentUrls()
87
FILE * fd = GetFD("w");
89
for (int i=0;i<MAX_URLS;i++) {
91
fprintf(fd, "%s\n", mURLs[i]);
96
for (int i=0;i<MAX_URLS;i++) {
102
char * CMostRecentUrls::GetURL(int aInx)
104
if (aInx < mNumURLs) {
111
void CMostRecentUrls::AddURL(const char * aURL)
114
strncpy(szTemp, aURL, sizeof(szTemp));
115
szTemp[sizeof(szTemp) - 1] = '\0';
117
// check to see if an existing url matches the one passed in
118
for (int i=0; i<MAX_URLS-1; i++)
122
if(strcmpi(mURLs[i], szTemp) == 0)
127
// if there was a match "i" will point to matching url entry
128
// if not i will be MAX_URLS-1
130
// move all url entries before this one down
137
mURLs[i] = _strdup(mURLs[i-1]);
140
// place this url at the top
143
mURLs[0] = _strdup(szTemp);