| 1 |
/* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */ |
| 2 |
|
| 3 |
/* AbiSource Application Framework |
| 4 |
* Copyright (C) 1998 AbiSource, Inc. |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2 |
| 9 |
* of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 |
* 02110-1301 USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
/* |
| 23 |
* Port to Maemo Development Platform |
| 24 |
* Author: INdT - Renato Araujo <[email protected]> |
| 25 |
*/ |
| 26 |
|
| 27 |
#ifdef HAVE_CONFIG_H |
| 28 |
#include "config.h" |
| 29 |
#endif |
| 30 |
|
| 31 |
#include <glib.h> |
| 32 |
#include <gtk/gtk.h> |
| 33 |
|
| 34 |
#include <stdlib.h> |
| 35 |
#include <stdio.h> |
| 36 |
#include <string.h> |
| 37 |
#include <strings.h> |
| 38 |
#include <unistd.h> |
| 39 |
|
| 40 |
#include <sys/stat.h> |
| 41 |
|
| 42 |
#include <fontconfig/fontconfig.h> |
| 43 |
|
| 44 |
#include "ut_debugmsg.h" |
| 45 |
#include "ut_path.h" |
| 46 |
#include "ut_string.h" |
| 47 |
#include "ut_uuid.h" |
| 48 |
|
| 49 |
#include "xap_UnixDialogHelper.h" |
| 50 |
#include "xap_Strings.h" |
| 51 |
#include "xap_UnixApp.h" |
| 52 |
#include "xap_FakeClipboard.h" |
| 53 |
#include "gr_UnixImage.h" |
| 54 |
#include "xap_Unix_TB_CFactory.h" |
| 55 |
#include "xap_Prefs.h" |
| 56 |
#include "xap_UnixEncodingManager.h" |
| 57 |
|
| 58 |
#include "gr_UnixCairoGraphics.h" |
| 59 |
#include <glib/gstdio.h> |
| 60 |
#include <gsf/gsf-utils.h> |
| 61 |
#include <goffice/goffice.h> |
| 62 |
#if !GTK_CHECK_VERSION(3,0,0) |
| 63 |
#include "gr_UnixPangoPixmapGraphics.h" |
| 64 |
#endif |
| 65 |
|
| 66 |
#ifdef WITH_GNOMEVFS |
| 67 |
#include <libgnomevfs/gnome-vfs.h> |
| 68 |
#endif |
| 69 |
|
| 70 |
#include "gr_CairoNullGraphics.h" |
| 71 |
static CairoNull_Graphics * nullgraphics = NULL; |
| 72 |
|
| 73 |
/*****************************************************************/ |
| 74 |
|
| 75 |
XAP_UnixApp::XAP_UnixApp(const char * szAppName) |
| 76 |
: XAP_App(szAppName), |
| 77 |
m_dialogFactory(this), |
| 78 |
m_controlFactory(), |
| 79 |
m_szTmpFile(NULL) |
| 80 |
{ |
| 81 |
int fc_inited = FcInit(); |
| 82 |
UT_UNUSED(fc_inited); // TODO actually deal with the error here |
| 83 |
UT_ASSERT(fc_inited); |
| 84 |
|
| 85 |
_setAbiSuiteLibDir(); |
| 86 |
|
| 87 |
memset(&m_geometry, 0, sizeof(m_geometry)); |
| 88 |
|
| 89 |
// create an instance of UT_UUIDGenerator or appropriate derrived class |
| 90 |
_setUUIDGenerator(new UT_UUIDGenerator()); |
| 91 |
|
| 92 |
// register graphics allocator |
| 93 |
GR_GraphicsFactory * pGF = getGraphicsFactory(); |
| 94 |
UT_ASSERT( pGF ); |
| 95 |
|
| 96 |
if(pGF) |
| 97 |
{ |
| 98 |
bool bSuccess; |
| 99 |
bSuccess = pGF->registerClass(GR_UnixCairoGraphics::graphicsAllocator, |
| 100 |
GR_UnixCairoGraphics::graphicsDescriptor, |
| 101 |
GR_UnixCairoGraphics::s_getClassId()); |
| 102 |
|
| 103 |
UT_ASSERT( bSuccess ); |
| 104 |
|
| 105 |
if(bSuccess) |
| 106 |
{ |
| 107 |
pGF->registerAsDefault(GR_UnixCairoGraphics::s_getClassId(), true); |
| 108 |
} |
| 109 |
|
| 110 |
bSuccess = pGF->registerClass(CairoNull_Graphics::graphicsAllocator, |
| 111 |
CairoNull_Graphics::graphicsDescriptor, |
| 112 |
CairoNull_Graphics::s_getClassId()); |
| 113 |
UT_ASSERT( bSuccess ); |
| 114 |
|
| 115 |
#if !GTK_CHECK_VERSION(3,0,0) |
| 116 |
bSuccess = pGF->registerClass(GR_UnixPangoPixmapGraphics::graphicsAllocator, |
| 117 |
GR_UnixPangoPixmapGraphics::graphicsDescriptor, |
| 118 |
GR_UnixPangoPixmapGraphics::s_getClassId()); |
| 119 |
|
| 120 |
if(bSuccess) |
| 121 |
{ |
| 122 |
pGF->registerAsDefault(GR_UnixPangoPixmapGraphics::s_getClassId(), false); |
| 123 |
} |
| 124 |
|
| 125 |
UT_ASSERT( bSuccess ); |
| 126 |
#endif |
| 127 |
/* We need to link CairoNull_Graphics because the AbiCommand |
| 128 |
* plugin uses it. |
| 129 |
* |
| 130 |
* We do not need to keep an instance of it around though, as the |
| 131 |
* plugin constructs its own (I wonder if there is a more elegant way |
| 132 |
* to force the linker into including it). |
| 133 |
*/ |
| 134 |
{ |
| 135 |
GR_CairoNullGraphicsAllocInfo ai; |
| 136 |
nullgraphics = |
| 137 |
(CairoNull_Graphics*) XAP_App::getApp()->newGraphics((UT_uint32)GRID_CAIRO_NULL, ai); |
| 138 |
|
| 139 |
delete nullgraphics; |
| 140 |
nullgraphics = NULL; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
XAP_UnixApp::~XAP_UnixApp() |
| 146 |
{ |
| 147 |
removeTmpFile(); |
| 148 |
// FcFini(); |
| 149 |
} |
| 150 |
|
| 151 |
void XAP_UnixApp::removeTmpFile(void) |
| 152 |
{ |
| 153 |
if(m_szTmpFile) |
| 154 |
{ |
| 155 |
if(g_file_test(m_szTmpFile,G_FILE_TEST_EXISTS)) |
| 156 |
{ |
| 157 |
// |
| 158 |
// Remove the tempfile if it exists |
| 159 |
// |
| 160 |
g_unlink(m_szTmpFile); |
| 161 |
delete [] m_szTmpFile; |
| 162 |
} |
| 163 |
} |
| 164 |
m_szTmpFile = NULL; |
| 165 |
} |
| 166 |
|
| 167 |
bool XAP_UnixApp::initialize(const char * szKeyBindingsKey, const char * szKeyBindingsDefaultValue) |
| 168 |
{ |
| 169 |
// let our base class do it's thing. |
| 170 |
|
| 171 |
XAP_App::initialize(szKeyBindingsKey, szKeyBindingsDefaultValue); |
| 172 |
|
| 173 |
libgoffice_init(); |
| 174 |
|
| 175 |
#ifdef WITH_GNOMEVFS |
| 176 |
gnome_vfs_init(); |
| 177 |
#endif |
| 178 |
|
| 179 |
// do any thing we need here... |
| 180 |
|
| 181 |
return true; |
| 182 |
} |
| 183 |
|
| 184 |
void XAP_UnixApp::shutdown() |
| 185 |
{ |
| 186 |
libgoffice_shutdown(); |
| 187 |
} |
| 188 |
|
| 189 |
void XAP_UnixApp::reallyExit() |
| 190 |
{ |
| 191 |
|
| 192 |
gtk_main_quit(); |
| 193 |
} |
| 194 |
|
| 195 |
UT_sint32 XAP_UnixApp::makeDirectory(const char * szPath, const UT_sint32 mode ) const |
| 196 |
{ |
| 197 |
return mkdir(szPath, mode); |
| 198 |
} |
| 199 |
|
| 200 |
XAP_DialogFactory * XAP_UnixApp::getDialogFactory() |
| 201 |
{ |
| 202 |
return &m_dialogFactory; |
| 203 |
} |
| 204 |
|
| 205 |
XAP_Toolbar_ControlFactory * XAP_UnixApp::getControlFactory() |
| 206 |
{ |
| 207 |
return &m_controlFactory; |
| 208 |
} |
| 209 |
|
| 210 |
void XAP_UnixApp::setWinGeometry(int x, int y, UT_uint32 width, UT_uint32 height, |
| 211 |
UT_uint32 flags) |
| 212 |
{ |
| 213 |
// TODO : do some range checking? |
| 214 |
m_geometry.x = x; |
| 215 |
m_geometry.y = y; |
| 216 |
m_geometry.width = width; |
| 217 |
m_geometry.height = height; |
| 218 |
m_geometry.flags = flags; |
| 219 |
} |
| 220 |
|
| 221 |
void XAP_UnixApp::getWinGeometry(int * x, int * y, UT_uint32 * width, |
| 222 |
UT_uint32 * height, UT_uint32 * flags) |
| 223 |
{ |
| 224 |
UT_return_if_fail(x && y && width && height); |
| 225 |
*x = m_geometry.x; |
| 226 |
*y = m_geometry.y; |
| 227 |
*width = m_geometry.width; |
| 228 |
*height = m_geometry.height; |
| 229 |
*flags = m_geometry.flags; |
| 230 |
} |
| 231 |
|
| 232 |
// This should be removed at some time. |
| 233 |
void XAP_UnixApp::migrate (const char *oldName, const char *newName, const char *path) const |
| 234 |
{ |
| 235 |
if (path && newName && oldName && (*oldName == '/')) |
| 236 |
{ |
| 237 |
char *old = new char[strlen(path) - strlen(newName) + strlen(oldName)]; |
| 238 |
|
| 239 |
if (old) |
| 240 |
{ |
| 241 |
size_t len = strrchr(path, '/') - path; |
| 242 |
strncpy(old, path, len); |
| 243 |
old[len] = 0; |
| 244 |
strcat(old, oldName); |
| 245 |
|
| 246 |
if (g_access(old, F_OK) == 0) |
| 247 |
{ |
| 248 |
UT_WARNINGMSG(("Renaming: %s -> %s\n", old, path)); |
| 249 |
g_rename(old, path); |
| 250 |
} |
| 251 |
|
| 252 |
delete[] old; |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
const char * XAP_UnixApp::getUserPrivateDirectory() |
| 258 |
{ |
| 259 |
/* return a pointer to a static buffer */ |
| 260 |
static char *buf = NULL; |
| 261 |
|
| 262 |
if (buf == NULL) |
| 263 |
{ |
| 264 |
const char * szAbiDir = "abiword"; |
| 265 |
const char * szCfgDir = ".config"; |
| 266 |
|
| 267 |
const char * szXDG = getenv("XDG_CONFIG_HOME"); |
| 268 |
if (!szXDG || !*szXDG) { |
| 269 |
const char * szHome = getenv("HOME"); |
| 270 |
if (!szHome || !*szHome) |
| 271 |
szHome = "./"; |
| 272 |
|
| 273 |
buf = new char[strlen(szHome)+strlen(szCfgDir)+strlen(szAbiDir)+4]; |
| 274 |
|
| 275 |
strcpy(buf, szHome); |
| 276 |
if (buf[strlen(buf)-1] != '/') |
| 277 |
strcat(buf, "/"); |
| 278 |
strcat(buf, szCfgDir); |
| 279 |
} else { |
| 280 |
buf = new char[strlen(szXDG)+strlen(szAbiDir)+4]; |
| 281 |
strcpy(buf, szXDG); |
| 282 |
} |
| 283 |
|
| 284 |
strcat(buf, "/"); |
| 285 |
strcat(buf, szAbiDir); |
| 286 |
|
| 287 |
#ifdef PATH_MAX |
| 288 |
if (strlen(buf) >= PATH_MAX) |
| 289 |
DELETEPV(buf); |
| 290 |
#endif |
| 291 |
|
| 292 |
// migration / legacy |
| 293 |
migrate("/AbiSuite", szAbiDir, buf); |
| 294 |
} |
| 295 |
|
| 296 |
return buf; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
void XAP_UnixApp::_setAbiSuiteLibDir() |
| 301 |
{ |
| 302 |
// FIXME: this code sucks hard |
| 303 |
|
| 304 |
char * buf = NULL; |
| 305 |
|
| 306 |
// see if ABIWORD_DATADIR was set in the environment |
| 307 |
const char * sz = getenv("ABIWORD_DATADIR"); |
| 308 |
if (sz && *sz) |
| 309 |
{ |
| 310 |
int len = strlen(sz); |
| 311 |
buf = (gchar *)g_malloc(len+1); |
| 312 |
strcpy(buf,sz); |
| 313 |
char * p = buf; |
| 314 |
if ( (p[0]=='"') && (p[len-1]=='"') ) |
| 315 |
{ |
| 316 |
// trim leading and trailing DQUOTES |
| 317 |
p[len-1]=0; |
| 318 |
p++; |
| 319 |
len -= 2; |
| 320 |
} |
| 321 |
if (p[len-1]=='/') // trim trailing slash |
| 322 |
p[len-1] = 0; |
| 323 |
XAP_App::_setAbiSuiteLibDir(p); |
| 324 |
g_free(buf); |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
// otherwise, use the hard-coded value |
| 329 |
XAP_App::_setAbiSuiteLibDir(getAbiSuiteHome()); |
| 330 |
|
| 331 |
return; |
| 332 |
} |
| 333 |
|
| 334 |
////////////////////////////////////////////////////////////////// |
| 335 |
////////////////////////////////////////////////////////////////// |
| 336 |
|
| 337 |
void XAP_UnixApp::setTimeOfLastEvent(UT_uint32 eventTime) |
| 338 |
{ |
| 339 |
m_eventTime = eventTime; |
| 340 |
} |
| 341 |
|