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: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Rod Spears <rods@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 //
00040 // CMostRecentUrls object is responsible for keeping track of the
00041 // 16 most recently used URLs. It stores this list in a file named
00042 // "urls.txt" on a per profile basis in the user's profile directory
00043 //
00044 // The constructor loads the URL list
00045 // The destructor saves the URL list
00046 //
00047 
00048 #include "StdAfx.h"
00049 #include "nsIFile.h"
00050 #include "nsILocalFile.h"
00051 #include "nsAppDirectoryServiceDefs.h"
00052 #include "MostRecentUrls.h"
00053 
00054 //--------------------------------------------------------
00055 //-- CMostRecentUrls
00056 //--------------------------------------------------------
00057 CMostRecentUrls::CMostRecentUrls() :
00058         mNumURLs(0)
00059 {
00060        for (int i=0;i<MAX_URLS;i++) {
00061               mURLs[i] = NULL;
00062        }
00063 
00064        FILE * fd = GetFD("r");
00065        if (fd) {
00066               char line[512];
00067               while (fgets(line, 512, fd)) {
00068                      if (strlen(line) > 1) {
00069                             line[strlen(line)-1] = 0;
00070                             mURLs[mNumURLs++] = _strdup(line);
00071                      }
00072               }
00073               fclose(fd);
00074        }
00075 
00076 }
00077 
00078 FILE * CMostRecentUrls::GetFD(const char * aMode) 
00079 {
00080     FILE * fd = nsnull;
00081     nsCOMPtr<nsIFile> file;
00082     nsresult rv = NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, getter_AddRefs(file));
00083     if (NS_SUCCEEDED(rv)) {
00084         nsCOMPtr<nsILocalFile> local_file(do_QueryInterface(file));
00085         local_file->AppendNative(NS_LITERAL_CSTRING("urls.txt"));
00086         local_file->OpenANSIFileDesc(aMode, &fd);
00087     }
00088 
00089     return fd;
00090 }
00091 
00092 CMostRecentUrls::~CMostRecentUrls() 
00093 {
00094     FILE * fd = GetFD("w");
00095     if (fd) {
00096         for (int i=0;i<MAX_URLS;i++) {
00097             if(mURLs[i])
00098                 fprintf(fd, "%s\n", mURLs[i]);
00099         }
00100     fclose(fd);
00101     }
00102 
00103     for (int i=0;i<MAX_URLS;i++) {
00104         if(mURLs[i])
00105           free(mURLs[i]);
00106     }
00107 }
00108 
00109 char * CMostRecentUrls::GetURL(int aInx)
00110 {
00111     if (aInx < mNumURLs) {
00112         return mURLs[aInx];
00113     }
00114 
00115     return NULL;
00116 }
00117 
00118 void CMostRecentUrls::AddURL(const char * aURL)
00119 {
00120        TCHAR szTemp[512];
00121        strcpy(szTemp, aURL);
00122 
00123        // check to see if an existing url matches the one passed in
00124        int i = 0;
00125        for (; i<MAX_URLS-1; i++)
00126        {
00127         if(mURLs[i])
00128         {
00129             if(strcmpi(mURLs[i], szTemp) == 0)
00130                 break; 
00131         }
00132        }
00133 
00134     // if there was a match "i" will point to matching url entry
00135     // if not i will be MAX_URLS-1
00136 
00137     // move all url entries before this one down
00138        for (; i>0; i--)
00139        {
00140         if(mURLs[i])
00141           free(mURLs[i]);
00142 
00143         if(mURLs[i-1])  
00144             mURLs[i] = _strdup(mURLs[i-1]);
00145        }
00146 
00147        // place this url at the top
00148     if(mURLs[0])
00149         free(mURLs[0]);
00150     mURLs[0] = _strdup(szTemp);
00151 }