/*
 * Copyright 2022 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 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 along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef PROCINFO_H
#define PROCINFO_H

#include <QObject>
#include <QApplication>
#include <QFile>
#include <QTimer>
#include <QThread>
#include <kwindowsystem.h>
#include <KWindowInfo>
#include <QProcess>
#include <QDebug>
#include <QDir>
#include <fstream>
#include <iostream>
#include "processinfo.h"

class QFileSystemWatcher;
class ProcessInfo;
/**
 * @brief The ProcInfo class 对于进程的工具类
 */
class ProcInfo : public QObject
{
    Q_OBJECT
public:
    explicit ProcInfo(quint32 pid, QObject *parent = nullptr);
    ///
    /// \brief isShell 判断当前进程启动的是不是脚本
    /// \return
    ///
    bool isShell(int pid);

    /**
     * @brief isKmreApp 判断当前进程启动的是不是移动运行应用（kmre）
     * @param pid 进程pid
     * @return
     */
    bool isKmreApp(int pid);

    /**
     * @brief isTerminalApp
     * @param pid 判断当前进程启动的是不是终端运行应用
     * @return
     */
    bool isTerminalApp(int pid);

    /**
     * @brief isTencentApp 判断当前进程启动的是不是腾讯应用
     * @param pid   应用进程pid
     * @return
     */
    bool isTencentApp(int pid);

    /**
     * @brief getCmdlineFromPid
     * @param pid 根据当前应用的pid获取Cmdline
     * @return
     */
    QString getCmdlineFromPid(int pid);

    int getPid(int pid);

    int getTerAppPPid(int pid);

    QString status();

    ///
    /// \brief guiAppPid 获取当前进程启动的二进制的pid
    /// \note 该函数是个阻塞函数，最差的情况会阻塞10s
    /// \return 如果当前进程启动的是脚本的话则返回脚本的启动的进程的pid
    ///  如果脚本未启动任何进程，返回-1
    ///  否则直接返回 \memberof m_pid
    ///
    int guiAppPid();

    static bool isWindow(int pid);

    static bool isXorg(int pid);
    static QList<quint32> pidOfCmdName(const QString &cmdName);
    static QList<quint64> getChildrenPid(const quint64 &pid);
    static QList<quint64> getTasksFromPid(const quint64 &appPid);
private:
    quint32 m_pid;
    const QString m_procPath;
};

#endif // PROCINFO_H
