Back to index

lightning-sunbird  0.9+nobinonly
MostRecentUrls.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: Mozilla-sample-code 1.0
00004  *
00005  * Copyright (c) 2002 Netscape Communications Corporation and
00006  * other contributors
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a
00009  * copy of this Mozilla sample software and associated documentation files
00010  * (the "Software"), to deal in the Software without restriction, including
00011  * without limitation the rights to use, copy, modify, merge, publish,
00012  * distribute, sublicense, and/or sell copies of the Software, and to permit
00013  * persons to whom the Software is furnished to do so, subject to the
00014  * following conditions:
00015  *
00016  * The above copyright notice and this permission notice shall be included
00017  * in all copies or substantial portions of the Software.
00018  *
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00020  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00022  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00024  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00025  * DEALINGS IN THE SOFTWARE.
00026  *
00027  * Contributor(s):
00028  *   Rod Spears <rods@netscape.com>
00029  *
00030  * ***** END LICENSE BLOCK ***** */
00031 
00032 //
00033 // CMostRecentUrls object is responsible for keeping track of the
00034 // 16 most recently used URLs. It stores this list in a file named
00035 // "urls.txt" on a per profile basis in the user's profile directory
00036 //
00037 // The constructor loads the URL list
00038 // The destructor saves the URL list
00039 //
00040 
00041 #include "StdAfx.h"
00042 #include "nsIFile.h"
00043 #include "nsILocalFile.h"
00044 #include "nsServiceManagerUtils.h"
00045 #include "nsAppDirectoryServiceDefs.h"
00046 #include "nsDirectoryServiceUtils.h"
00047 #include "MostRecentUrls.h"
00048 
00049 //--------------------------------------------------------
00050 //-- CMostRecentUrls
00051 //--------------------------------------------------------
00052 CMostRecentUrls::CMostRecentUrls() :
00053         mNumURLs(0)
00054 {
00055     for (int i=0;i<MAX_URLS;i++) {
00056         mURLs[i] = NULL;
00057     }
00058 
00059     FILE * fd = GetFD("r");
00060     if (fd) {
00061         char line[512];
00062         while (fgets(line, 512, fd)) {
00063             if (strlen(line) > 1) {
00064                 line[strlen(line)-1] = 0;
00065                 mURLs[mNumURLs++] = _strdup(line);
00066             }
00067         }
00068         fclose(fd);
00069     }
00070 
00071 }
00072 
00073 FILE * CMostRecentUrls::GetFD(const char * aMode) 
00074 {
00075     FILE * fd = nsnull;
00076     nsCOMPtr<nsIFile> file;
00077     nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(file));
00078     if (NS_SUCCEEDED(rv)) {
00079         nsCOMPtr<nsILocalFile> local_file(do_QueryInterface(file));
00080         local_file->AppendNative(nsEmbedCString("urls.txt"));
00081         local_file->OpenANSIFileDesc(aMode, &fd);
00082     }
00083 
00084     return fd;
00085 }
00086 
00087 CMostRecentUrls::~CMostRecentUrls() 
00088 {
00089     FILE * fd = GetFD("w");
00090     if (fd) {
00091         for (int i=0;i<MAX_URLS;i++) {
00092             if(mURLs[i])
00093                 fprintf(fd, "%s\n", mURLs[i]);
00094         }
00095     fclose(fd);
00096     }
00097 
00098     for (int i=0;i<MAX_URLS;i++) {
00099         if(mURLs[i])
00100           free(mURLs[i]);
00101     }
00102 }
00103 
00104 char * CMostRecentUrls::GetURL(int aInx)
00105 {
00106     if (aInx < mNumURLs) {
00107         return mURLs[aInx];
00108     }
00109 
00110     return NULL;
00111 }
00112 
00113 void CMostRecentUrls::AddURL(const char * aURL)
00114 {
00115     char szTemp[512];
00116     strncpy(szTemp, aURL, sizeof(szTemp));
00117     szTemp[sizeof(szTemp) - 1] = '\0';
00118 
00119     // check to see if an existing url matches the one passed in
00120     int i = 0;
00121     for (; i<MAX_URLS-1; i++)
00122     {
00123         if(mURLs[i])
00124         {
00125             if(strcmpi(mURLs[i], szTemp) == 0)
00126                 break; 
00127         }
00128     }
00129 
00130     // if there was a match "i" will point to matching url entry
00131     // if not i will be MAX_URLS-1
00132 
00133     // move all url entries before this one down
00134     for (; i>0; i--)
00135     {
00136         if(mURLs[i])
00137           free(mURLs[i]);
00138 
00139         if(mURLs[i-1])  
00140             mURLs[i] = _strdup(mURLs[i-1]);
00141     }
00142 
00143     // place this url at the top
00144     if(mURLs[0])
00145         free(mURLs[0]);
00146     mURLs[0] = _strdup(szTemp);
00147 }