~ubuntu-branches/ubuntu/jaunty/psi/jaunty

« back to all changes in this revision

Viewing changes to src/applicationinfo.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <QString>
 
2
#include <QDir>
 
3
#include <QFile>
 
4
 
 
5
#ifdef Q_WS_X11
 
6
#include <sys/stat.h> // chmod
 
7
#endif
 
8
 
 
9
#ifdef Q_WS_WIN
 
10
#include <windows.h>
 
11
#endif
 
12
 
 
13
#ifdef Q_WS_MAC
 
14
#include <sys/stat.h> // chmod
 
15
#include <CoreServices/CoreServices.h>
 
16
#endif
 
17
 
 
18
#include "applicationinfo.h"
 
19
#include "profiles.h"
 
20
#ifdef HAVE_CONFIG
 
21
#include "config.h"
 
22
#endif
 
23
 
 
24
// Constants. These should be moved to a more 'dynamically changeable'
 
25
// place (like an external file loaded through the resources system)
 
26
// Should also be overridable through an optional file.
 
27
 
 
28
#define PROG_NAME "Psi"
 
29
//#define PROG_VERSION "0.11-dev" " (" __DATE__ ")"; //CVS Builds are dated
 
30
#define PROG_VERSION "0.11";
 
31
#define PROG_CAPS_NODE "http://psi-im.org/caps";
 
32
#define PROG_CAPS_VERSION "0.11-dev-rev8";
 
33
#define PROG_OPTIONS_NS "http://psi-im.org/options";
 
34
#define PROG_STORAGE_NS "http://psi-im.org/storage";
 
35
 
 
36
#if defined(Q_WS_X11) && !defined(PSI_DATADIR)
 
37
#define PSI_DATADIR "/usr/local/share/psi"
 
38
#endif
 
39
 
 
40
 
 
41
QString ApplicationInfo::name()
 
42
{
 
43
        return PROG_NAME;
 
44
}
 
45
 
 
46
 
 
47
QString ApplicationInfo::version()
 
48
{
 
49
        return PROG_VERSION;
 
50
}
 
51
 
 
52
QString ApplicationInfo::capsNode()
 
53
{
 
54
        return PROG_CAPS_NODE;
 
55
}
 
56
 
 
57
QString ApplicationInfo::capsVersion()
 
58
{
 
59
        return PROG_CAPS_VERSION;
 
60
}
 
61
 
 
62
QString ApplicationInfo::optionsNS()
 
63
{
 
64
        return PROG_OPTIONS_NS;
 
65
}
 
66
 
 
67
QString ApplicationInfo::storageNS()
 
68
{
 
69
        return PROG_STORAGE_NS;
 
70
}       
 
71
 
 
72
QString ApplicationInfo::resourcesDir()
 
73
{
 
74
#if defined(Q_WS_X11)
 
75
        return PSI_DATADIR;
 
76
#elif defined(Q_WS_WIN)
 
77
        return qApp->applicationDirPath();
 
78
#elif defined(Q_WS_MAC)
 
79
        // FIXME: Clean this up (remko)
 
80
  // System routine locates resource files. We "know" that Psi.icns is
 
81
  // in the Resources directory.
 
82
  QString resourcePath;
 
83
  CFBundleRef mainBundle = CFBundleGetMainBundle();
 
84
  CFStringRef resourceCFStringRef
 
85
      = CFStringCreateWithCString( NULL, "application.icns",
 
86
                                   kCFStringEncodingASCII );
 
87
  CFURLRef resourceURLRef = CFBundleCopyResourceURL( mainBundle,
 
88
                                                     resourceCFStringRef,
 
89
                                                     NULL,
 
90
                                                     NULL );
 
91
  if ( resourceURLRef ) {
 
92
    CFStringRef resourcePathStringRef =
 
93
    CFURLCopyFileSystemPath( resourceURLRef, kCFURLPOSIXPathStyle );
 
94
    const char* resourcePathCString =
 
95
      CFStringGetCStringPtr( resourcePathStringRef, kCFStringEncodingASCII );
 
96
    if ( resourcePathCString ) {
 
97
      resourcePath.setLatin1( resourcePathCString );
 
98
    } else { // CFStringGetCStringPtr failed; use fallback conversion
 
99
      CFIndex bufferLength = CFStringGetLength( resourcePathStringRef ) + 1;
 
100
      char* resourcePathCString = new char[ bufferLength ];
 
101
      Boolean conversionSuccess =
 
102
        CFStringGetCString( resourcePathStringRef,
 
103
                            resourcePathCString, bufferLength,
 
104
                            kCFStringEncodingASCII );
 
105
      if ( conversionSuccess ) {
 
106
        resourcePath = resourcePathCString;
 
107
      }
 
108
      delete [] resourcePathCString;  // I own this
 
109
    }
 
110
    CFRelease( resourcePathStringRef ); // I own this
 
111
  }
 
112
  // Remove the tail component of the path
 
113
  if ( ! resourcePath.isNull() ) {
 
114
    QFileInfo fileInfo( resourcePath );
 
115
    resourcePath = fileInfo.dirPath( true );
 
116
  }
 
117
  return resourcePath;
 
118
#endif
 
119
}
 
120
 
 
121
 
 
122
/** \brief return psi's private read write data directory
 
123
  * unix+mac: $HOME/.psi
 
124
  * environment variable "PSIDATADIR" overrides
 
125
  */
 
126
QString ApplicationInfo::homeDir()
 
127
{
 
128
        // Try the environment override first
 
129
        char *p = getenv("PSIDATADIR");
 
130
        if(p)
 
131
                return p;
 
132
 
 
133
#if defined(Q_WS_X11)
 
134
        QDir proghome(QDir::homeDirPath() + "/.psi");
 
135
        if(!proghome.exists()) {
 
136
                QDir home = QDir::home();
 
137
                home.mkdir(".psi");
 
138
                chmod(QFile::encodeName(proghome.path()), 0700);
 
139
        }
 
140
        return proghome.path();
 
141
#elif defined(Q_WS_WIN)
 
142
        QString base;
 
143
 
 
144
        // Windows 9x
 
145
        if(QDir::homeDirPath() == QDir::rootDirPath())
 
146
                base = ".";
 
147
        // Windows NT/2K/XP variant
 
148
        else
 
149
                base = QDir::homeDirPath();
 
150
 
 
151
        // no trailing slash
 
152
        if(base.at(base.length()-1) == '/')
 
153
                base.truncate(base.length()-1);
 
154
 
 
155
        QDir proghome(base + "/PsiData");
 
156
        if(!proghome.exists()) {
 
157
                QDir home(base);
 
158
                home.mkdir("PsiData");
 
159
        }
 
160
 
 
161
        return proghome.path();
 
162
#elif defined(Q_WS_MAC)
 
163
        QDir proghome(QDir::homeDirPath() + "/.psi");
 
164
        if(!proghome.exists()) {
 
165
                QDir home = QDir::home();
 
166
                home.mkdir(".psi");
 
167
                chmod(QFile::encodeName(proghome.path()), 0700);
 
168
        }
 
169
 
 
170
        return proghome.path();
 
171
#endif
 
172
}
 
173
 
 
174
QString ApplicationInfo::historyDir()
 
175
{
 
176
        QDir history(pathToProfile(activeProfile) + "/history");
 
177
        if (!history.exists()) {
 
178
                QDir profile(pathToProfile(activeProfile));
 
179
                profile.mkdir("history");
 
180
        }
 
181
 
 
182
        return history.path();
 
183
}
 
184
 
 
185
QString ApplicationInfo::vCardDir()
 
186
{
 
187
        QDir vcard(pathToProfile(activeProfile) + "/vcard");
 
188
        if (!vcard.exists()) {
 
189
                QDir profile(pathToProfile(activeProfile));
 
190
                profile.mkdir("vcard");
 
191
        }
 
192
 
 
193
        return vcard.path();
 
194
}
 
195
 
 
196
QString ApplicationInfo::profilesDir()
 
197
{
 
198
        QString profiles_dir(homeDir() + "/profiles");
 
199
        QDir d(profiles_dir);
 
200
        if(!d.exists()) {
 
201
                QDir d(homeDir());
 
202
                d.mkdir("profiles");
 
203
        }
 
204
        return profiles_dir;
 
205
}