Skip to content

Commit

Permalink
tempory fixes for read and set paths on macos
Browse files Browse the repository at this point in the history
  • Loading branch information
0sxr00t committed Apr 27, 2020
1 parent e6f5f89 commit bad66c1
Show file tree
Hide file tree
Showing 2 changed files with 250 additions and 226 deletions.
319 changes: 169 additions & 150 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* MystiQ - a C++/Qt5 gui frontend for ffmpeg
* Copyright (C) 2011-2019 Maikel Llamaret Heredia <[email protected]>
* Copyright (C) 2011-2019 Maikel Llamaret Heredia
* <[email protected]>
*
* 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
Expand All @@ -15,196 +16,214 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "converter/exepath.h"
#include "converter/ffmpeginterface.h"
#include "converter/mediaprobe.h"
#include "services/constants.h"
#include "services/notification.h"
#include "services/paths.h"
#include "ui/mainwindow.h"
#include <QApplication>
#include <QDebug>
#include <QLocale>
#include <QTranslator>
#include <QDir>
#include <QSettings>
#include <QLocale>
#include <QMessageBox>
#include "ui/mainwindow.h"
#include "services/paths.h"
#include "converter/ffmpeginterface.h"
#include "converter/mediaprobe.h"
#include "converter/exepath.h"
#include "services/notification.h"
#include "services/constants.h"
#include <QSettings>
#include <QStandardPaths>
#include <QTranslator>
#include <iostream>

/**
* @brief Find the absolute path of the translation of the current locale.
*
* @return the absolute path of the translation or an empty string if file not found.
* @return the absolute path of the translation or an empty string if file not
* found.
* @note This function must be called after @c Paths has been initialized.
*/
static QString find_translation_file()
{
QString locale = QLocale::system().name(); // language code + country code (xx_XX)
QString language = locale.mid(0, 2); // language code (first two chars of locale)
QString translation_file_basename =
//QDir(Paths::translationPath()).absoluteFilePath("mystiq_");
QDir(":/translations/").absoluteFilePath("mystiq_");

// look for mystiq_xx_XX.qm in the translation directory
QString translation_language_country = translation_file_basename + locale + ".qm";
if (QFile(translation_language_country).exists())
return translation_language_country;

// look for mystiq_xx.qm in the translation directory
QString translation_language = translation_file_basename + language + ".qm";
if (QFile(translation_language).exists())
return translation_language;

// translation for current locale not found, return empty string
return "";
static QString find_translation_file() {
QString locale =
QLocale::system().name(); // language code + country code (xx_XX)
QString language =
locale.mid(0, 2); // language code (first two chars of locale)
QString translation_file_basename =
// QDir(Paths::translationPath()).absoluteFilePath("mystiq_");
QDir(":/translations/").absoluteFilePath("mystiq_");

// look for mystiq_xx_XX.qm in the translation directory
QString translation_language_country =
translation_file_basename + locale + ".qm";
if (QFile(translation_language_country).exists())
return translation_language_country;

// look for mystiq_xx.qm in the translation directory
QString translation_language = translation_file_basename + language + ".qm";
if (QFile(translation_language).exists())
return translation_language;

// translation for current locale not found, return empty string
return "";
}

/**
* @brief Load program constants from constants.xml.
* @return true on success, false on failure
*/
static bool load_constants(QApplication& app)
{
static bool load_constants(QApplication &app) {
#ifdef DATA_PATH
QString app_path = QString(DATA_PATH);
(void)app; // eliminate "variable not used" warning
QString app_path = QString(DATA_PATH);
(void)app; // eliminate "variable not used" warning
#else
QString app_path = app.applicationDirPath();
QString app_path = app.applicationDirPath();
#endif
QString constant_xml_filename = ":/other/constants.xml";

// open constant xml file
QFile constant_xml(constant_xml_filename);
constant_xml.open(QIODevice::ReadOnly);
if (!constant_xml.isOpen()) {
qCritical() << "Failed to read file: " << constant_xml_filename;
QMessageBox::critical(nullptr, "MystiQ",
QString("Cannot load %1. The program will exit now.")
.arg(constant_xml_filename));
return false;
}

qDebug() << "Reading file: " << constant_xml_filename;
// parse the xml file
if (!Constants::readFile(constant_xml)) {
QMessageBox::critical(nullptr, "MystiQ",
QString("%1 contains error(s). "
"Reinstall the application may solve the problem.")
QString constant_xml_filename = ":/other/constants.xml";

// open constant xml file
QFile constant_xml(constant_xml_filename);
constant_xml.open(QIODevice::ReadOnly);
if (!constant_xml.isOpen()) {
qCritical() << "Failed to read file: " << constant_xml_filename;
QMessageBox::critical(nullptr, "MystiQ",
QString("Cannot load %1. The program will exit now.")
.arg(constant_xml_filename));
return false;
}
return false;
}

qDebug() << "Reading file: " << constant_xml_filename;
// parse the xml file
if (!Constants::readFile(constant_xml)) {
QMessageBox::critical(
nullptr, "MystiQ",
QString("%1 contains error(s). "
"Reinstall the application may solve the problem.")
.arg(constant_xml_filename));
return false;
}

return true;
return true;
}

// register an external tool for use
// returns whether the tool can be successfully invoked
static bool register_tool(const char *id, const char *name)
{
QString exefile = name; // default: use the program in PATH
static bool register_tool(const char *id, const char *name) {
QString exefile = name; // default: use the program in PATH

#ifdef TOOLS_IN_DATA_PATH // Search external tools in <datapath>/tools
#ifdef Q_OS_WIN32 // executable files must end with .exe on MS Windows
exefile = Paths::dataFileName("tools/%1.exe").arg(name);
#else
exefile = Paths::dataFileName("tools/%1").arg(name);
#ifdef Q_OS_WIN32 // executable files must end with .exe on MS Windows
exefile = Paths::dataFileName("tools/%1.exe").arg(name);
#endif // Q_OS_WIN32
#ifdef Q_OS_MACOS // executable files must end without extension on MacOS
exefile = Paths::dataFileName("tools/%1").arg(name);
#endif // Q_OS_MACOS
#endif // TOOLS_IN_DATA_PATH
ExePath::setPath(id, exefile);
if (ExePath::checkProgramAvailability(id))
return true;
// failed to invoke the program
ExePath::setPath(id, ""); // unset the tool
return false;

ExePath::setPath(id, exefile);
if (ExePath::checkProgramAvailability(id)) {
// std::cout << exefile.toStdString();
return true;
}

// failed to invoke the program
ExePath::setPath(id, ""); // unset the tool
// std::cout << "Falso";
std::cout << exefile.toStdString() + "\n";
return false;
}

static bool register_tool(const char *name)
{
return register_tool(name, name);
static bool register_tool(const char *name) {
return register_tool(name, name);
}

static void register_external_tools()
{
static void register_external_tools() {

bool mac = false;

#ifdef Q_OS_MAC
mac = true;
#endif

if (mac) {
// ExePath::loadSettings();

ExePath::setPath("ffmpeg", "/usr/local/bin/ffmpeg");
ExePath::setPath("ffprobe", "/usr/local/bin/ffprobe");
ExePath::setPath("sox", "/usr/local/bin/sox");

} else {
// load user settings for the tools
ExePath::loadSettings();
// If the setting of ffmpeg is not available, register it again.
// If "ffmpeg" doesn't exist on the system, try "avconv" instead.
ExePath::checkProgramAvailability("ffmpeg")
|| register_tool("ffmpeg")
|| register_tool("ffmpeg", "avconv");
ExePath::checkProgramAvailability("ffmpeg") || register_tool("ffmpeg") ||
register_tool("ffmpeg", "avconv");
// same as "ffmpeg" (try "avprobe" if "ffprobe" not available)
ExePath::checkProgramAvailability("ffprobe")
|| register_tool("ffprobe")
|| register_tool("ffprobe", "avprobe");
ExePath::checkProgramAvailability("ffprobe") || register_tool("ffprobe") ||
register_tool("ffprobe", "avprobe");
// same as above
// these tools have no alternative names
register_tool("sox");
}
}

int main(int argc, char *argv[])
{
// Create Application.
QApplication app(argc, argv);

if (!load_constants(app)) {
app.exec();
return EXIT_FAILURE;
}

// Register QSettings information.
app.setOrganizationName("mystiq");
QSettings::setDefaultFormat(QSettings::IniFormat);
if (Constants::getBool("Portable")) {
// Portable App: Save settings in <exepath>/mystiq.ini.
QSettings::setPath(QSettings::IniFormat, QSettings::UserScope
, app.applicationDirPath());
} else {
// Save settings to the default Qt location
}
qDebug() << "Settings filename: " + QSettings().fileName();

Paths::setAppPath(app.applicationDirPath());

register_external_tools();

// Construct a string list containing all input filenames.
QStringList inputFiles(app.arguments());
inputFiles.removeFirst(); // Exclude self executable name.

// Setup translation
QTranslator translator;
QString translation_filename = find_translation_file();
if (!translation_filename.isEmpty()) {
qDebug() << "Translation file: " << translation_filename;
translator.load(translation_filename);
app.installTranslator(&translator);
}

// Load translation for Qt library
QTranslator translator_qt;
translator_qt.load("qt_" + QLocale::system().name(), Paths::qtTranslationPath());
app.installTranslator(&translator_qt);

// Setup notification
Notification::init();
if (!Notification::setType(Notification::TYPE_LIBNOTIFY))
Notification::setType(Notification::TYPE_NOTIFYSEND);

// Create main window.
MainWindow window(nullptr, inputFiles);
window.show();

// Execute the main loop
int status = app.exec();

// Tear down notification
Notification::release();

#ifndef TOOLS_IN_DATA_PATH
// Save path settings
ExePath::saveSettings();
#endif
int main(int argc, char *argv[]) {
// Create Application.
QApplication app(argc, argv);

if (!load_constants(app)) {
app.exec();
return EXIT_FAILURE;
}
// Register QSettings information.
app.setOrganizationName("mystiq");
QSettings::setDefaultFormat(QSettings::IniFormat);

if (Constants::getBool("Portable")) {
// Portable App: Save settings in <exepath>/mystiq.ini.
QSettings::setPath(QSettings::IniFormat, QSettings::UserScope,
app.applicationDirPath());
} else {

// Save settings to the default Qt location
}
qDebug() << "Settings filename: " + QSettings().fileName();

Paths::setAppPath(app.applicationDirPath());

register_external_tools();

// Construct a string list containing all input filenames.
QStringList inputFiles(app.arguments());
inputFiles.removeFirst(); // Exclude self executable name.

// Setup translation
QTranslator translator;
QString translation_filename = find_translation_file();
if (!translation_filename.isEmpty()) {
qDebug() << "Translation file: " << translation_filename;
translator.load(translation_filename);
app.installTranslator(&translator);
}

// Load translation for Qt library
QTranslator translator_qt;
translator_qt.load("qt_" + QLocale::system().name(),
Paths::qtTranslationPath());
app.installTranslator(&translator_qt);

// Setup notification
Notification::init();
if (!Notification::setType(Notification::TYPE_LIBNOTIFY))
Notification::setType(Notification::TYPE_NOTIFYSEND);

// Create main window.
MainWindow window(nullptr, inputFiles);
window.show();

// Execute the main loop
int status = app.exec();

// Tear down notification
Notification::release();

#ifdef TOOLS_IN_DATA_PATH
ExePath::saveSettings();
#endif // TOOLS_IN_DATA_PATH

return status;
return status;
}
Loading

0 comments on commit bad66c1

Please sign in to comment.