Try to avoid crash on Mac OS during setup

... by moving setupFavLink to a place only called
if the folder to be referenced exists for sure.
This commit is contained in:
Daniel Molkentin 2013-03-21 12:01:37 +01:00
parent 4a57957420
commit 337c6d3e4e
5 changed files with 49 additions and 42 deletions

View file

@ -21,13 +21,6 @@
#include "mirall/inotify.h"
#include "mirall/theme.h"
#ifdef Q_OS_MAC
#include <CoreServices/CoreServices.h>
#endif
#ifdef Q_OS_WIN
#include <shlobj.h>
#endif
#include <QDesktopServices>
#include <QtCore>
@ -178,38 +171,6 @@ QString FolderMan::unescapeAlias( const QString& alias ) const
return a;
}
void FolderMan::setupFavLink(const QString &folder)
{
#ifdef Q_OS_WIN
// Windows Explorer: Place under "Favorites" (Links)
wchar_t path[MAX_PATH];
SHGetSpecialFolderPath(0, path, CSIDL_PROFILE, FALSE);
QString profile = QDir::fromNativeSeparators(QString::fromWCharArray(path));
QDir folderDir(QDir::fromNativeSeparators(folder));
QString linkName = profile+QLatin1String("/Links/") + folderDir.dirName() + QLatin1String(".lnk");
if (!QFile::link(folder, linkName))
qDebug() << Q_FUNC_INFO << "linking" << folder << "to" << linkName << "failed!";
#elif defined (Q_OS_MAC)
// Finder: Place under "Places"
QString folderUrl = QUrl::fromLocalFile(folder).toString();
CFStringRef folderCFStr = CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar *>(folderUrl.unicode()),
folder.length());
CFURLRef urlRef = CFURLCreateWithString(NULL, folderCFStr, 0);
LSSharedFileListRef placesItems = LSSharedFileListCreate(0, kLSSharedFileListFavoriteItems, 0);
if (placesItems) {
//Insert an item to the list.
LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(placesItems,
kLSSharedFileListItemBeforeFirst, 0, 0,
urlRef, 0, 0);
if (item)
CFRelease(item);
}
CFRelease(placesItems);
CFRelease(folderCFStr);
CFRelease(urlRef);
#endif
}
// filename is the name of the file only, it does not include
// the configuration directory path
Folder* FolderMan::setupFolderFromConfigFile(const QString &file) {
@ -456,8 +417,6 @@ void FolderMan::addFolderDefinition( const QString& backend, const QString& alia
settings.setValue(QString::fromLatin1("%1/connection").arg(escapedAlias), Theme::instance()->appName());
settings.setValue(QString::fromLatin1("%1/onlyThisLAN").arg(escapedAlias), onlyThisLAN );
settings.sync();
setupFavLink(sourceFolder);
}
void FolderMan::removeAllFolderDefinitions()

View file

@ -111,7 +111,6 @@ private:
// finds all folder configuration files
// and create the folders
int setupKnownFolders();
void setupFavLink(const QString& folder);
void terminateCurrentSync();
// Escaping of the alias which is used in QSettings AND the file

View file

@ -17,6 +17,7 @@
#include "mirall/owncloudinfo.h"
#include "mirall/folderman.h"
#include "mirall/credentialstore.h"
#include "mirall/utility.h"
#include <QtCore>
#include <QProcess>
@ -421,6 +422,7 @@ void OwncloudSetupWizard::setupLocalSyncFolder()
} else {
QString res = tr("Creating local sync folder %1... ").arg(_localFolder);
if( fi.mkpath( _localFolder ) ) {
Utility::setupFavLink( _localFolder );
// FIXME: Create a local sync folder.
res += tr("ok");
} else {

View file

@ -12,6 +12,20 @@
*/
#include "utility.h"
#include <QDir>
#include <QFile>
#include <QUrl>
#include <QDebug>
#ifdef Q_OS_MAC
#include <CoreServices/CoreServices.h>
#endif
#ifdef Q_OS_WIN
#include <shlobj.h>
#endif
namespace Mirall {
QString Utility::formatFingerprint( const QByteArray& fmhash )
@ -30,4 +44,36 @@ QString Utility::formatFingerprint( const QByteArray& fmhash )
return fp;
}
void Utility::setupFavLink(const QString &folder)
{
#ifdef Q_OS_WIN
// Windows Explorer: Place under "Favorites" (Links)
wchar_t path[MAX_PATH];
SHGetSpecialFolderPath(0, path, CSIDL_PROFILE, FALSE);
QString profile = QDir::fromNativeSeparators(QString::fromWCharArray(path));
QDir folderDir(QDir::fromNativeSeparators(folder));
QString linkName = profile+QLatin1String("/Links/") + folderDir.dirName() + QLatin1String(".lnk");
if (!QFile::link(folder, linkName))
qDebug() << Q_FUNC_INFO << "linking" << folder << "to" << linkName << "failed!";
#elif defined (Q_OS_MAC)
// Finder: Place under "Places"
QString folderUrl = QUrl::fromLocalFile(folder).toString();
CFStringRef folderCFStr = CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar *>(folderUrl.unicode()),
folder.length());
CFURLRef urlRef = CFURLCreateWithString(NULL, folderCFStr, 0);
LSSharedFileListRef placesItems = LSSharedFileListCreate(0, kLSSharedFileListFavoriteItems, 0);
if (placesItems) {
//Insert an item to the list.
LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(placesItems,
kLSSharedFileListItemBeforeFirst, 0, 0,
urlRef, 0, 0);
if (item)
CFRelease(item);
}
CFRelease(placesItems);
CFRelease(folderCFStr);
CFRelease(urlRef);
#endif
}
}

View file

@ -24,6 +24,7 @@ class Utility
{
public:
static QString formatFingerprint( const QByteArray& );
static void setupFavLink( const QString &folder );
};
}