]> git.lyx.org Git - lyx.git/blobdiff - src/support/FileName.cpp
Fix bug 9798.
[lyx.git] / src / support / FileName.cpp
index 929c4165bebd541bead6ce6e7a6a61774e12f89d..189c5645a459ac0ae4d9d72e46188a2ed482a2df 100644 (file)
@@ -89,18 +89,17 @@ struct FileName::Private
 {
        Private() {}
 
-       Private(string const & abs_filename) : fi(toqstr(abs_filename))
+       Private(string const & abs_filename) : fi(toqstr(handleTildeName(abs_filename)))
        {
                name = fromqstr(fi.absoluteFilePath());
                fi.setCaching(fi.exists() ? true : false);
        }
        ///
-       inline void refresh() 
+       inline void refresh()
        {
                fi.refresh();
        }
 
-
        static
        bool isFilesystemEqual(QString const & lhs, QString const & rhs)
        {
@@ -108,6 +107,14 @@ struct FileName::Private
                        Qt::CaseSensitive : Qt::CaseInsensitive) == 0;
        }
 
+       static
+       string const handleTildeName(string const & name)
+       {
+               return name == "~" ? Package::get_home_dir().absFileName() :
+                       prefixIs(name, "~/") ? Package::get_home_dir().absFileName() + name.substr(1) :
+                       name;
+       }
+
        /// The absolute file name in UTF-8 encoding.
        std::string name;
        ///
@@ -171,7 +178,7 @@ bool FileName::empty() const
 
 bool FileName::isAbsolute(string const & name)
 {
-       QFileInfo fi(toqstr(name));
+       QFileInfo fi(toqstr(Private::handleTildeName(name)));
        return fi.isAbsolute();
 }
 
@@ -190,7 +197,7 @@ string FileName::realPath() const
 
 void FileName::set(string const & name)
 {
-       d->fi.setFile(toqstr(name));
+       d->fi.setFile(toqstr(Private::handleTildeName(name)));
        d->name = fromqstr(d->fi.absoluteFilePath());
        //LYXERR(Debug::FILES, "FileName::set(" << name << ')');
        LATTEST(empty() || isAbsolute(d->name));
@@ -257,7 +264,7 @@ bool FileName::renameTo(FileName const & name) const
 
 bool FileName::moveTo(FileName const & name) const
 {
-       LYXERR(Debug::FILES, "Moving " << name << " to " << *this);
+       LYXERR(Debug::FILES, "Moving " << *this << " to " << name);
        QFile::remove(name.d->fi.absoluteFilePath());
 
        bool success = QFile::rename(d->fi.absoluteFilePath(),
@@ -538,7 +545,7 @@ unsigned long FileName::checksum() const
        // FIXME: This code is not ready with Qt4.4.2,
        // see http://www.lyx.org/trac/ticket/5293
        // FIXME: should we check if the MapExtension extension is supported?
-       // see QAbstractFileEngine::supportsExtension() and 
+       // see QAbstractFileEngine::supportsExtension() and
        // QAbstractFileEngine::MapExtension)
        QFile qf(fi.filePath());
        if (!qf.open(QIODevice::ReadOnly))
@@ -622,12 +629,12 @@ static bool rmdir(QFileInfo const & fi)
                        continue;
                bool removed;
                if (list.at(i).isDir()) {
-                       LYXERR(Debug::FILES, "Removing dir " 
+                       LYXERR(Debug::FILES, "Removing dir "
                                << fromqstr(list.at(i).absoluteFilePath()));
                        removed = rmdir(list.at(i));
                }
                else {
-                       LYXERR(Debug::FILES, "Removing file " 
+                       LYXERR(Debug::FILES, "Removing file "
                                << fromqstr(list.at(i).absoluteFilePath()));
                        removed = dir.remove(list.at(i).fileName());
                }
@@ -636,7 +643,7 @@ static bool rmdir(QFileInfo const & fi)
                        LYXERR0("Could not delete "
                                << fromqstr(list.at(i).absoluteFilePath()));
                }
-       } 
+       }
        QDir parent = fi.absolutePath();
        success &= parent.rmdir(fi.fileName());
        return success;
@@ -925,7 +932,7 @@ string DocFileName::mangledFileName(string const & dir) const
        typedef map<string, string> MangledMap;
        static MangledMap mangledNames;
        static Mutex mangledMutex;
-       // this locks both access to mangledNames and counter below 
+       // this locks both access to mangledNames and counter below
        Mutex::Locker lock(&mangledMutex);
        MangledMap::const_iterator const it = mangledNames.find(absFileName());
        if (it != mangledNames.end())