GUI: Refactoring the loading of libraries in mainwindow

This commit is contained in:
Daniel Marjamäki 2013-12-31 10:46:44 +01:00
parent 803182bf45
commit 14d65f212f
2 changed files with 34 additions and 20 deletions

View File

@ -512,6 +512,28 @@ void MainWindow::AddIncludeDirs(const QStringList &includeDirs, Settings &result
} }
} }
bool MainWindow::LoadLibrary(Library *library, QString filename)
{
// Try to load the library from the project folder..
if (mProject) {
QString path = QFileInfo(mProject->GetProjectFile()->GetFilename()).canonicalPath();
if (library->load(NULL, (path+"/"+filename).toLatin1()))
return true;
}
// Try to load the library from the application folder..
QString path = QFileInfo(QCoreApplication::applicationFilePath()).canonicalPath();
if (library->load(NULL, (path+"/"+filename).toLatin1()))
return true;
// Try to load the library from the cfg subfolder..
path = path + "/cfg";
if (library->load(NULL, (path+"/"+filename).toLatin1()))
return true;
return false;
}
Settings MainWindow::GetCppcheckSettings() Settings MainWindow::GetCppcheckSettings()
{ {
Settings result; Settings result;
@ -533,23 +555,8 @@ Settings MainWindow::GetCppcheckSettings()
QStringList libraries = pfile->GetLibraries(); QStringList libraries = pfile->GetLibraries();
foreach(QString library, libraries) { foreach(QString library, libraries) {
const QString filename = library + ".cfg"; const QString filename = library + ".cfg";
if (!LoadLibrary(&result.library, filename))
// Try to load the library from the project folder.. QMessageBox::information(this, tr("Information"), tr("Failed to load the selected library %1").arg(filename));
QString path = QFileInfo(pfile->GetFilename()).canonicalPath();
if (result.library.load("", (path+"/"+filename).toLatin1()))
continue;
// Try to load the library from the application folder..
path = QFileInfo(QCoreApplication::applicationFilePath()).canonicalPath();
if (result.library.load("", (path+"/"+filename).toLatin1()))
continue;
// Try to load the library from the cfg subfolder..
path = path + "/cfg";
if (result.library.load("", (path+"/"+filename).toLatin1()))
continue;
QMessageBox::information(this, tr("Information"), tr("Failed to load the selected library %1").arg(filename));
} }
QStringList suppressions = pfile->GetSuppressions(); QStringList suppressions = pfile->GetSuppressions();
@ -591,11 +598,10 @@ Settings MainWindow::GetCppcheckSettings()
result.standards.c = mSettings->value(SETTINGS_STD_C99, true).toBool() ? Standards::C99 : (mSettings->value(SETTINGS_STD_C11, false).toBool() ? Standards::C11 : Standards::C89); result.standards.c = mSettings->value(SETTINGS_STD_C99, true).toBool() ? Standards::C99 : (mSettings->value(SETTINGS_STD_C11, false).toBool() ? Standards::C11 : Standards::C89);
result.standards.posix = mSettings->value(SETTINGS_STD_POSIX, false).toBool(); result.standards.posix = mSettings->value(SETTINGS_STD_POSIX, false).toBool();
const QString applicationFilePath = QCoreApplication::applicationFilePath(); bool std = LoadLibrary(&result.library, "std.cfg");
bool std = result.library.load(applicationFilePath.toLatin1(), "std.cfg");
bool posix = true; bool posix = true;
if (result.standards.posix) if (result.standards.posix)
posix = result.library.load(applicationFilePath.toLatin1(), "posix.cfg"); posix = LoadLibrary(&result.library, "posix.cfg");
if (!std || !posix) if (!std || !posix)
QMessageBox::warning(this, tr("Error"), tr("Failed to load %1. Your Cppcheck installation is broken.").arg(!std ? "std.cfg" : "posix.cfg")); QMessageBox::warning(this, tr("Error"), tr("Failed to load %1. Your Cppcheck installation is broken.").arg(!std ? "std.cfg" : "posix.cfg"));

View File

@ -424,6 +424,14 @@ private:
*/ */
void LoadProjectFile(const QString &filePath); void LoadProjectFile(const QString &filePath);
/**
* @brief Load library file
* @param Library library to use
* @param filename filename (no path)
* @return True if successful
*/
bool LoadLibrary(Library *library, QString filename);
/** /**
* @brief Update project MRU items in File-menu. * @brief Update project MRU items in File-menu.
*/ */