⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filelauncher.cpp

📁 Last change: 2008-02-03 This is the source code of KCeasy。
💻 CPP
字号:
/*
This file is part of KCeasy (http://www.kceasy.com)
Copyright (C) 2002-2004 Markus Kern <mkern@kceasy.com>

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 2
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.
*/
//---------------------------------------------------------------------------
#include <vcl.h>
#include <KCeasy.h>
#pragma hdrstop

#include "FileLauncher.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

// a white list would be safer of course... then again even mp3s can overflow
// your winamp...

// dangerous extension we warn the user of
static const char* DangerousExtensions[] =
{
    "exe",
    "dll",
    "bat",
    "hlp", // help file
    "chm", // compiled help file
    "scr", // screensaver
    "url", // internet shortcut
    "doc", // ms word
    "xls", // ms excel
    "ppt", // ms powerpoint
    "msi", // ms installer
    "msp", // ms installer patch
    "mdb", // ms access db
    "mde", // ms access db
    "ade", // ms access project
    "adp", // ms access project
    NULL
};

// dangerous extension we don't even allow opening
static const char* TooDangerousExtensions[] =
{
    "lnk", // shortcut
    "reg", // registry export
    "com", // legacy dos binary
    "pif", // dos binary config file, interpreted as exe by the oh so clever os
    "vb",  // visual basic script
    "vbe", // visual basic script
    "vbs", // visual basic script
    "bas", // visual basic
    "cmd", // nt command script
    "cpl", // control panel dll
    "hta", // html application
    "js",  // javascript
    "jse", // javascript
    "inf", // setup information
    "ins", // internet naming service
    "isp", // internet communication settings
    "crt", // security certificate
    "shs", // shell scrap object
    "shb", // shell scrap object
    "sct", // windows script component
    "wsc", // windows script component
    "wsf", // windows script file
    "wsh", // windows scripting host
    "asp", // active server pages
    "pcd", // photo cd image, microsoft visual compiled script
    "mst", // microsoft visual test source
    "msc", // microsoft common console document
    NULL
};

// public, static

bool TFileLauncher::IsFileDangerous(const string& File)
{
    string Extension = GetCleanExtension(File);

    if(Extension == "")
        return false;

    for(char** Ext = (char**)DangerousExtensions; *Ext; Ext++) {
        if(Extension == *Ext)
            return true;
    }

    return false;
}

bool TFileLauncher::IsFileTooDangerous(const string& File)
{
    string Extension = GetCleanExtension(File);

    if(Extension == "")
        return false;

    for(char** Ext = (char**)TooDangerousExtensions; *Ext; Ext++) {
        if(Extension == *Ext)
            return true;
    }

    return false;
}

bool TFileLauncher::UserWantsToLaunch(const string& File)
{
    // TRANSLATOR: Message box when user tries to open a file with dangerous extension.
    if(MessageDlg(_("You are about to open a potentially dangerous file type. The file may contain a virus or trojan.\n"
                    "Are you sure you want to continue?"),mtWarning,TMsgDlgButtons() << mbYes << mbNo,0) == mrYes)
    {
        return true;
    }

    return false;
}

void TFileLauncher::ShowLaunchFailedMsg(const string& File)
{
    // TRANSLATOR: Message box when user tried to open file externally and it failed.
    MessageDlg(_("The file could not be opened.\n"
                 "The most likely cause for this is that you have no application associated with this file type."),
               mtError,TMsgDlgButtons() << mbOK,0);
}

void TFileLauncher::ShowTorrentLaunchFailedMsg(const string& File)
{
    // TRANSLATOR: Message box when user tried launch torrent externally and it failed.
    MessageDlg(_("The torrent could not be launched.\n"
                 "Make sure you have a Bittorrent client installed and associated with torrent files."),
               mtError,TMsgDlgButtons() << mbOK,0);
}

bool TFileLauncher::LaunchFile(const string& File)
{
    if(IsFileTooDangerous(File))
        return false;

    string Dir = DirFromPath(File);

    if((DWORD)::ShellExecute(NULL,NULL,File.c_str(),NULL,Dir.c_str(),SW_SHOW) <= 32)
        return false;

    return true;
}

bool TFileLauncher::LaunchUrl(const string& Url)
{
    if((DWORD)::ShellExecute(NULL,"open",Url.c_str(),NULL,NULL,SW_SHOW) <= 32)
        return false;

    return true;
}

bool TFileLauncher::ExploreDir(const string& Path)
{
    if((DWORD)::ShellExecute(NULL,"open",Path.c_str(),NULL,Path.c_str(),SW_SHOW) <= 32)
        return false;

    return true;
}

bool TFileLauncher::ExploreFile(const string& FilePath)
{
    char WinDir[MAX_PATH];
    if(::GetWindowsDirectory(WinDir,sizeof(WinDir)) == 0)
        return false;

    string Dir = DirFromPath(FilePath);
    string Explorer = string(WinDir) + "\\explorer.exe";
    string Params = "/select," + FilePath;

    if((DWORD)::ShellExecute(NULL,"open",Explorer.c_str(),Params.c_str(),Dir.c_str(),SW_SHOW) <= 32)
        return false;

    return true;
}

// private, static

string TFileLauncher::GetCleanExtension(const string& File)
{
    int pos;

    if((pos = File.rfind('.')) == -1)
        return "";

    if(pos < 2)
        return "";

    string Extension = File.substr(pos+1);

    // remove leading white space
    if((pos = Extension.find_first_not_of(" \t")) == -1)
        return "";
    Extension = Extension.substr(pos);

    // remove trailing white space
    if((pos = Extension.find_last_not_of(" \t")) == -1)
        return "";
    Extension = Extension.substr(0,pos+1);

    return Extension;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -