You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
239 lines
5.3 KiB
239 lines
5.3 KiB
15 years ago
|
////////////////////////////////////////////////////////////////////////////////
|
||
|
//
|
||
|
// Namespace : KFI::Misc
|
||
|
// Author : Craig Drummond
|
||
|
// Project : K Font Installer
|
||
|
// Creation Date : 01/05/2001
|
||
|
// Version : $Revision$ $Date$
|
||
|
//
|
||
|
////////////////////////////////////////////////////////////////////////////////
|
||
|
//
|
||
|
// This program is free software; you can redistribute it and/or
|
||
|
// modify it under the terms of the GNU General Public License
|
||
|
// as published by the Free Software Foundation; either version 2
|
||
|
// of the License, or (at your option) any later version.
|
||
|
//
|
||
|
// This program is distributed in the hope that it will be useful,
|
||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
// GNU General Public License for more details.
|
||
|
//
|
||
|
// You should have received a copy of the GNU General Public License
|
||
|
// along with this program; if not, write to the Free Software
|
||
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
//
|
||
|
////////////////////////////////////////////////////////////////////////////////
|
||
|
// (C) Craig Drummond, 2001, 2002, 2003, 2004
|
||
|
////////////////////////////////////////////////////////////////////////////////
|
||
|
|
||
|
#include "Misc.h"
|
||
15 years ago
|
#include <tqfile.h>
|
||
15 years ago
|
#include <kprocess.h>
|
||
|
#include <kstandarddirs.h>
|
||
|
#include <klargefile.h>
|
||
12 years ago
|
#include <tdeio/netaccess.h>
|
||
15 years ago
|
#include <unistd.h>
|
||
|
|
||
|
namespace KFI
|
||
|
{
|
||
|
|
||
|
namespace Misc
|
||
|
{
|
||
|
|
||
15 years ago
|
TQString linkedTo(const TQString &i)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString d;
|
||
15 years ago
|
|
||
|
if(isLink(i))
|
||
|
{
|
||
|
char buffer[1000];
|
||
15 years ago
|
int n=readlink(TQFile::encodeName(i), buffer, 1000);
|
||
15 years ago
|
|
||
|
if(n!=-1)
|
||
|
{
|
||
|
buffer[n]='\0';
|
||
|
d=buffer;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return d;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString dirSyntax(const TQString &d)
|
||
15 years ago
|
{
|
||
|
if(!d.isEmpty())
|
||
|
{
|
||
15 years ago
|
TQString ds(d);
|
||
15 years ago
|
|
||
13 years ago
|
ds.replace("//", "/");
|
||
15 years ago
|
|
||
13 years ago
|
int slashPos=ds.findRev('/');
|
||
15 years ago
|
|
||
|
if(slashPos!=(((int)ds.length())-1))
|
||
|
ds.append('/');
|
||
|
|
||
|
return ds;
|
||
|
}
|
||
|
|
||
|
return d;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString xDirSyntax(const TQString &d)
|
||
15 years ago
|
{
|
||
|
if(!d.isEmpty())
|
||
|
{
|
||
15 years ago
|
TQString ds(d);
|
||
15 years ago
|
|
||
13 years ago
|
ds.replace("//", "/");
|
||
15 years ago
|
|
||
13 years ago
|
int slashPos=ds.findRev('/');
|
||
15 years ago
|
|
||
|
if(slashPos==(((int)ds.length())-1))
|
||
|
ds.remove(slashPos, 1);
|
||
|
return ds;
|
||
|
}
|
||
|
|
||
|
return d;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString getDir(const TQString &f)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString d(f);
|
||
15 years ago
|
|
||
13 years ago
|
int slashPos=d.findRev('/');
|
||
15 years ago
|
|
||
|
if(slashPos!=-1)
|
||
|
d.remove(slashPos+1, d.length());
|
||
|
|
||
|
return dirSyntax(d);
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString getFile(const TQString &f)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString d(f);
|
||
15 years ago
|
|
||
13 years ago
|
int slashPos=d.findRev('/');
|
||
15 years ago
|
|
||
|
if(slashPos!=-1)
|
||
|
d.remove(0, slashPos+1);
|
||
|
|
||
|
return d;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool createDir(const TQString &dir)
|
||
15 years ago
|
{
|
||
|
//
|
||
|
// Clear any umask before dir is created
|
||
|
mode_t oldMask=umask(0000);
|
||
12 years ago
|
bool status=TDEStandardDirs::makeDir(dir, DIR_PERMS);
|
||
15 years ago
|
// Reset umask
|
||
|
::umask(oldMask);
|
||
|
return status;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool doCmd(const TQString &cmd, const TQString &p1, const TQString &p2, const TQString &p3)
|
||
15 years ago
|
{
|
||
12 years ago
|
TDEProcess proc;
|
||
15 years ago
|
|
||
|
proc << cmd;
|
||
|
|
||
|
if(!p1.isEmpty())
|
||
|
proc << p1;
|
||
|
if(!p2.isEmpty())
|
||
|
proc << p2;
|
||
|
if(!p3.isEmpty())
|
||
|
proc << p3;
|
||
|
|
||
12 years ago
|
proc.start(TDEProcess::Block);
|
||
15 years ago
|
|
||
|
return proc.normalExit() && proc.exitStatus()==0;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString changeExt(const TQString &f, const TQString &newExt)
|
||
15 years ago
|
{
|
||
15 years ago
|
TQString newStr(f);
|
||
13 years ago
|
int dotPos=newStr.findRev('.');
|
||
15 years ago
|
|
||
|
if(-1==dotPos)
|
||
15 years ago
|
newStr+=TQChar('.')+newExt;
|
||
15 years ago
|
else
|
||
|
{
|
||
|
newStr.remove(dotPos+1, newStr.length());
|
||
|
newStr+=newExt;
|
||
|
}
|
||
|
return newStr;
|
||
|
}
|
||
|
|
||
15 years ago
|
void createBackup(const TQString &f)
|
||
15 years ago
|
{
|
||
15 years ago
|
const TQString constExt(".bak");
|
||
15 years ago
|
|
||
|
if(!fExists(f+constExt) && fExists(f))
|
||
|
doCmd("cp", "-f", f, f+constExt);
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// Get a list of files associated with a file, e.g.:
|
||
|
//
|
||
|
// File: /home/a/courier.pfa
|
||
|
//
|
||
|
// Associated: /home/a/courier.afm /home/a/courier.pfm
|
||
|
//
|
||
15 years ago
|
void getAssociatedUrls(const KURL &url, KURL::List &list, bool afmAndPfm, TQWidget *widget)
|
||
15 years ago
|
{
|
||
|
const char *afm[]={"afm", "AFM", "Afm", "AFm", "AfM", "aFM", "aFm", "afM", NULL},
|
||
|
*pfm[]={"pfm", "PFM", "Pfm", "PFm", "PfM", "pFM", "pFm", "pfM", NULL};
|
||
|
bool gotAfm=false,
|
||
|
localFile=url.isLocalFile();
|
||
|
int e;
|
||
|
|
||
|
for(e=0; afm[e]; ++e)
|
||
|
{
|
||
|
KURL statUrl(url);
|
||
12 years ago
|
TDEIO::UDSEntry uds;
|
||
15 years ago
|
|
||
|
statUrl.setPath(changeExt(url.path(), afm[e]));
|
||
|
|
||
12 years ago
|
if(localFile ? fExists(statUrl.path()) : TDEIO::NetAccess::stat(statUrl, uds, widget))
|
||
15 years ago
|
{
|
||
|
list.append(statUrl);
|
||
|
gotAfm=true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(afmAndPfm || !gotAfm)
|
||
|
for(e=0; pfm[e]; ++e)
|
||
|
{
|
||
|
KURL statUrl(url);
|
||
12 years ago
|
TDEIO::UDSEntry uds;
|
||
15 years ago
|
|
||
|
statUrl.setPath(changeExt(url.path(), pfm[e]));
|
||
12 years ago
|
if(localFile ? fExists(statUrl.path()) : TDEIO::NetAccess::stat(statUrl, uds, widget))
|
||
15 years ago
|
{
|
||
|
list.append(statUrl);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
15 years ago
|
time_t getTimeStamp(const TQString &item)
|
||
15 years ago
|
{
|
||
|
KDE_struct_stat info;
|
||
|
|
||
15 years ago
|
return !item.isEmpty() && 0==KDE_lstat(TQFile::encodeName(item), &info) ? info.st_mtime : 0;
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
15 years ago
|
bool check(const TQString &path, unsigned int fmt, bool checkW)
|
||
15 years ago
|
{
|
||
|
KDE_struct_stat info;
|
||
15 years ago
|
TQCString pathC(TQFile::encodeName(path));
|
||
15 years ago
|
|
||
|
return 0==KDE_lstat(pathC, &info) && (info.st_mode&S_IFMT)==fmt && (!checkW || 0==::access(pathC, W_OK));
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|