~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/embedding/tests/mfcembed/MostRecentUrls.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
5
 * Copyright (c) 2002 Netscape Communications Corporation and
 
6
 * other contributors
 
7
 *
 
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:
 
15
 *
 
16
 * The above copyright notice and this permission notice shall be included
 
17
 * in all copies or substantial portions of the Software.
 
18
 *
 
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.
 
26
 *
 
27
 * Contributor(s):
 
28
 *   Rod Spears <rods@netscape.com>   
 
29
 *
 
30
 * ***** END LICENSE BLOCK ***** */
 
31
 
 
32
//
 
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
 
36
//
 
37
// The constructor loads the URL list
 
38
// The destructor saves the URL list
 
39
//
 
40
 
 
41
#include "StdAfx.h"
 
42
#include "nsIFile.h"
 
43
#include "nsILocalFile.h"
 
44
#include "nsAppDirectoryServiceDefs.h"
 
45
#include "MostRecentUrls.h"
 
46
 
 
47
//--------------------------------------------------------
 
48
//-- CMostRecentUrls
 
49
//--------------------------------------------------------
 
50
CMostRecentUrls::CMostRecentUrls() :
 
51
        mNumURLs(0)
 
52
{
 
53
    for (int i=0;i<MAX_URLS;i++) {
 
54
        mURLs[i] = NULL;
 
55
    }
 
56
 
 
57
    FILE * fd = GetFD("r");
 
58
    if (fd) {
 
59
        char line[512];
 
60
        while (fgets(line, 512, fd)) {
 
61
            if (strlen(line) > 1) {
 
62
                line[strlen(line)-1] = 0;
 
63
                mURLs[mNumURLs++] = _strdup(line);
 
64
            }
 
65
        }
 
66
        fclose(fd);
 
67
    }
 
68
 
 
69
}
 
70
 
 
71
FILE * CMostRecentUrls::GetFD(const char * aMode) 
 
72
{
 
73
    FILE * fd = nsnull;
 
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);
 
80
    }
 
81
 
 
82
    return fd;
 
83
}
 
84
 
 
85
CMostRecentUrls::~CMostRecentUrls() 
 
86
{
 
87
    FILE * fd = GetFD("w");
 
88
    if (fd) {
 
89
        for (int i=0;i<MAX_URLS;i++) {
 
90
            if(mURLs[i])
 
91
                fprintf(fd, "%s\n", mURLs[i]);
 
92
        }
 
93
    fclose(fd);
 
94
    }
 
95
 
 
96
    for (int i=0;i<MAX_URLS;i++) {
 
97
        if(mURLs[i])
 
98
          free(mURLs[i]);
 
99
    }
 
100
}
 
101
 
 
102
char * CMostRecentUrls::GetURL(int aInx)
 
103
{
 
104
    if (aInx < mNumURLs) {
 
105
        return mURLs[aInx];
 
106
    }
 
107
 
 
108
    return NULL;
 
109
}
 
110
 
 
111
void CMostRecentUrls::AddURL(const char * aURL)
 
112
{
 
113
    char szTemp[512];
 
114
    strncpy(szTemp, aURL, sizeof(szTemp));
 
115
    szTemp[sizeof(szTemp) - 1] = '\0';
 
116
 
 
117
    // check to see if an existing url matches the one passed in
 
118
    for (int i=0; i<MAX_URLS-1; i++)
 
119
    {
 
120
        if(mURLs[i])
 
121
        {
 
122
            if(strcmpi(mURLs[i], szTemp) == 0)
 
123
                break; 
 
124
        }
 
125
    }
 
126
 
 
127
    // if there was a match "i" will point to matching url entry
 
128
    // if not i will be MAX_URLS-1
 
129
 
 
130
    // move all url entries before this one down
 
131
    for (; i>0; i--)
 
132
    {
 
133
        if(mURLs[i])
 
134
          free(mURLs[i]);
 
135
 
 
136
        if(mURLs[i-1])  
 
137
            mURLs[i] = _strdup(mURLs[i-1]);
 
138
    }
 
139
 
 
140
    // place this url at the top
 
141
    if(mURLs[0])
 
142
        free(mURLs[0]);
 
143
    mURLs[0] = _strdup(szTemp);
 
144
}