ukui-search/src/main.cpp

267 lines
9.4 KiB
C++
Raw Normal View History

/*
*
2021-01-29 11:43:07 +08:00
* Copyright (C) 2020, KylinSoft Co., Ltd.
*
* 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
2021-01-29 11:43:07 +08:00
* the Free Software Foundation, either version 3 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
2021-01-29 11:43:07 +08:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Authors: zhangjiaping <zhangjiaping@kylinos.cn>
* Modified by: zhangpengfei <zhangpengfei@kylinos.cn>
* Modified by: zhangzihao <zhangzihao@kylinos.cn>
*
*/
2020-12-21 16:26:59 +08:00
2020-12-30 14:42:04 +08:00
#include "mainwindow.h"
#include <QDesktopWidget>
#include <QFile>
#include <QScreen>
#include <QTranslator>
#include <QLocale>
#include <X11/Xlib.h>
#include <syslog.h>
#include <QObject>
#include "qt-single-application.h"
#include "qt-local-peer.h"
//#include "inotify-manager.h"
2020-12-30 15:31:36 +08:00
#include "libsearch.h"
#include "global-settings.h"
#include "xatom-helper.h"
//void handler(int){
// qDebug() << "Recieved SIGTERM!";
2021-01-23 11:32:18 +08:00
// GlobalSettings::getInstance()->setValue(INDEX_DATABASE_STATE, "2");
// GlobalSettings::getInstance()->setValue(CONTENT_INDEX_DATABASE_STATE, "2");
// GlobalSettings::getInstance()->setValue(INDEX_GENERATOR_NORMAL_EXIT, "2");
// GlobalSettings::getInstance()->setValue(INOTIFY_NORMAL_EXIT, "2");
// GlobalSettings::getInstance()->forceSync(INDEX_DATABASE_STATE);
// GlobalSettings::getInstance()->forceSync(CONTENT_INDEX_DATABASE_STATE);
// GlobalSettings::getInstance()->forceSync(INDEX_GENERATOR_NORMAL_EXIT);
// GlobalSettings::getInstance()->forceSync(INOTIFY_NORMAL_EXIT);
// qDebug() << "indexDataBaseStatus: " << GlobalSettings::getInstance()->getValue(INDEX_DATABASE_STATE).toString();
// qDebug() << "contentIndexDataBaseStatus: " << GlobalSettings::getInstance()->getValue(CONTENT_INDEX_DATABASE_STATE).toString();
2021-01-23 11:32:18 +08:00
// ::exit(0);
//// InotifyIndex::getInstance("/home")->~InotifyIndex();
// //wait linux kill this thread forcedly
//// while (true);
//}
void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QByteArray localMsg = msg.toLocal8Bit();
QByteArray currentTime = QTime::currentTime().toString().toLocal8Bit();
bool showDebug = true;
// QString logFilePath = QStandardPaths::writableLocation(QStandardPaths::TempLocation) + "/ukui-search.log";
// QString logFilePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.config/org.ukui/ukui-search/ukui-search.log";
QString logFilePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "/.config/org.ukui/ukui-search.log";
if (!QFile::exists(logFilePath)) {
showDebug = false;
}
FILE *log_file = nullptr;
if (showDebug) {
log_file = fopen(logFilePath.toLocal8Bit().constData(), "a+");
}
const char *file = context.file ? context.file : "";
const char *function = context.function ? context.function : "";
switch (type) {
case QtDebugMsg:
if (!log_file) {
break;
}
fprintf(log_file, "Debug: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function);
break;
case QtInfoMsg:
fprintf(log_file? log_file: stdout, "Info: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function);
break;
case QtWarningMsg:
fprintf(log_file? log_file: stderr, "Warning: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function);
break;
case QtCriticalMsg:
fprintf(log_file? log_file: stderr, "Critical: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function);
break;
case QtFatalMsg:
fprintf(log_file? log_file: stderr, "Fatal: %s: %s (%s:%u, %s)\n", currentTime.constData(), localMsg.constData(), file, context.line, function);
break;
}
if (log_file)
fclose(log_file);
}
void centerToScreen(QWidget* widget) {
if (!widget)
return;
QDesktopWidget* m = QApplication::desktop();
QRect desk_rect = m->screenGeometry(m->screenNumber(QCursor::pos()));
int desk_x = desk_rect.width();
int desk_y = desk_rect.height();
int x = widget->width();
int y = widget->height();
2021-01-11 20:49:42 +08:00
widget->move(desk_x / 2 - x / 2 + desk_rect.left(), desk_y / 2 - y / 2 + desk_rect.top());
}
2020-12-21 16:26:59 +08:00
int main(int argc, char *argv[])
{
unlink(UKUI_SEARCH_PIPE_PATH);
int retval = mkfifo(UKUI_SEARCH_PIPE_PATH, 0777);
if(retval == -1)
{
perror("creat fifo error\n");
assert(false);
return -1;
}
printf("create fifo success\n");
qInstallMessageHandler(messageOutput);
qRegisterMetaType<QPair<QString,QStringList>>("QPair<QString,QStringList>");
qRegisterMetaType<Document>("Document");
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
QtSingleApplication app("ukui-search", argc, argv);
app.setQuitOnLastWindowClosed(false);
if(app.isRunning())
{
app.sendMessage(QApplication::arguments().length() > 1 ? QApplication::arguments().at(1) : app.applicationFilePath());
qDebug() << QObject::tr("ukui-search is already running!");
return EXIT_SUCCESS;
}/*else {
QCommandLineParser parser;
QCommandLineOption debugOption({"d", "debug"}, QObject::tr("Display debug information"));
QCommandLineOption showsearch({"s", "show"}, QObject::tr("show search widget"));
parser.addOptions({debugOption, showsearch});
parser.process(app);
}*/
//here need to be modified
/*-------------ukuisearchdbus Test start-----------------*/
UkuiSearchQDBus usQDBus;
usQDBus.setInotifyMaxUserWatches();
/*-------------ukuisearchdbus Test End-----------------*/
//load chinese character and pinyin file to a Map
FileUtils::loadHanziTable("://index/pinyinWithoutTone.txt");
2020-12-26 21:11:17 +08:00
/*-------------InotyifyRefact Test Start---------------*/
// QTime t1 = QTime::currentTime();
// InotifyManagerRefact* imr = new InotifyManagerRefact("/home");
// imr->start();
// QTime t2 = QTime::currentTime();
// qDebug() << t1;
// qDebug() << t2;
2020-12-26 21:11:17 +08:00
/*-------------InotyifyRefact Test End-----------------*/
/*-------------content index Test Start---------------*/
// QTime t3 = QTime::currentTime();
// FileTypeFilter* ftf = new FileTypeFilter("/home");
// ftf->Test();
// QTime t4 = QTime::currentTime();
// delete ftf;
// ftf = nullptr;
// qDebug() << t3;
// qDebug() << t4;
/*-------------content index Test End-----------------*/
/*-------------文本搜索 Test start-----------------*/
2021-01-03 16:01:35 +08:00
// FileSearcher *search = new FileSearcher();
// search->onKeywordSearchContent("重要器官移植⑤白血病");
// search->onKeywordSearchContent("g,e,x");
/*-------------文本搜索 Test End-----------------*/
// 加载国际化文件
QTranslator translator;
try {
if (! translator.load("/usr/share/ukui-search/translations/" + QLocale::system().name())) throw -1;
app.installTranslator(&translator);
} catch (...) {
qDebug() << "Load translations file" << QLocale() << "failed!";
}
QTranslator qt_translator;
try {
if (! qt_translator.load(":/res/qt-translations/qt_zh_CN.qm")) throw -1;
app.installTranslator(&qt_translator);
} catch (...) {
qDebug() << "Load translations file" << QLocale() << "failed!";
}
MainWindow *w = new MainWindow;
QStringList arguments = QCoreApplication::arguments();
// centerToScreen(w);
w->moveToPanel();
//使用窗管的无边框策略
w->setProperty("useStyleWindowManager", false); //禁用拖动
MotifWmHints hints;
hints.flags = MWM_HINTS_FUNCTIONS|MWM_HINTS_DECORATIONS;
hints.functions = MWM_FUNC_ALL;
hints.decorations = MWM_DECOR_BORDER;
XAtomHelper::getInstance()->setWindowMotifHint(w->winId(), hints);
app.setActivationWindow(w);
if (QString::compare(QString("-s"), QString(QLatin1String(argv[1]))) == 0) {
2021-01-29 09:27:02 +08:00
w->moveToPanel();
w->show();
}
2020-12-29 20:18:36 +08:00
// if(arguments.size()>1)
// w->searchContent(arguments.at(1));
QObject::connect(&app, SIGNAL(messageReceived(const QString&)),w, SLOT(bootOptionsFilter(const QString&)));
// qDebug() << "main start";
// FirstIndex* fi = new FirstIndex("/home");
// fi->start();
qDebug() << "main start";
2021-03-05 08:43:02 +08:00
AppMatch::getAppMatch()->start();
AppMatch apm;
apm.start();
QThreadPool::globalInstance()->setExpiryTimeout(5);
// QThreadPool::globalInstance()->clear();
// setAutoDelete(true);
// FirstIndex fi("/home/zhangzihao/Desktop/qwerty");
// FirstIndex* fi = new FirstIndex("/home/zhangzihao/Desktop/qwerty");
FirstIndex fi("/home/zhangzihao/Desktop");
fi.start();
// fi.wait();
// fi->wait();
// fi->exit();
// delete fi;
// assert(false);
InotifyIndex* ii = InotifyIndex::getInstance("/home");
// InotifyIndex ii("/home");
ii->start();
// qDebug() << "sigset start!";
// sigset( SIGTERM, handler);
// qDebug() << "sigset end!";
return app.exec();
2020-12-21 16:26:59 +08:00
}