expander.cpp
来自「LINUX 下, 以 QT/KDE 写的档案管理员」· C++ 代码 · 共 1,225 行 · 第 1/3 页
CPP
1,225 行
//// C++ Implementation: expander//// Description: ////// Author: Jonas B�r (C) 2004//// Copyright: See COPYING file that comes with this distribution////#include <algorithm>#include "expander.h"#include "../krusader.h"#include "../krusaderview.h"#include "../panelmanager.h"#include "../Panel/listpanel.h"#include "../Panel/panelfunc.h"#include "../Panel/krview.h"#include "../Synchronizer/synchronizergui.h"#include "../Search/krsearchdialog.h"#include "../GUI/profilemanager.h"#include "../VFS/preservingcopyjob.h"#include "../KViewer/krviewer.h"#include "../krservices.h"#ifdef __KJSEMBED__#include "../KrJS/krjs.h"#endif#include <kdebug.h>#include <kinputdialog.h>#include <kstandarddirs.h>#include <kmessagebox.h>#include <ktempfile.h>#include <qstringlist.h>#include <qclipboard.h>#include <functional>using namespace std;#define NEED_PANEL if (panel==0) { panelMissingError(_expression,exp); return QString::null; }#include "tstring.h"QValueList<const exp_placeholder*>& Expander::_placeholder(){ static QValueList<const exp_placeholder*> ret; return ret;}void exp_placeholder::panelMissingError(const QString &s, Expander& exp){ exp.setError( Error(Error::S_FATAL,Error::C_ARGUMENT,i18n("Needed panel specification missing in expander %1").arg(s)) );}QStringList exp_placeholder::fileList(const ListPanel* const panel,const QString& type,const QString& mask,const bool ommitPath,const bool useUrl,Expander& exp,const QString& error){ QStringList items; if ( type.isEmpty() || type == "all" ) panel->view->getItemsByMask( mask, &items ); else if ( type == "files" ) panel->view->getItemsByMask( mask, &items, false, true ); else if ( type == "dirs" ) panel->view->getItemsByMask( mask, &items, true, false ); else if ( type == "selected" ) panel->view->getSelectedItems( &items ); else { setError(exp, Error(Error::S_FATAL,Error::C_ARGUMENT,i18n("Expander: Bad argument to %1: %2 is not valid item specifier").arg(error,type) ) ); return QString::null; } if ( !ommitPath ) { // add the current path // translate to urls using vfs KURL::List* list = panel->func->files()->vfs_getFiles(&items); items.clear(); // parse everything to a single qstring for (KURL::List::Iterator it = list->begin(); it != list->end(); ++it) { items.push_back(useUrl ? (*it).url() : (*it).path()); } delete list; } return items;}namespace {class exp_simpleplaceholder : public exp_placeholder{public: EXP_FUNC; virtual TagString expFunc ( const ListPanel*, const QStringList&, const bool&, Expander& ) const=0;};/** * expands %_Path% ('_' is replaced by 'a', 'o', 'r' or 'l' to indicate the active, other, right or left panel) with the path of the specified panel */class exp_Path : public exp_simpleplaceholder { static const exp_Path instance; exp_Path();public: SIMPLE_EXP_FUNC;};/** * expands %_Count% ('_' is replaced by 'a', 'o', 'r' or 'l' to indicate the active, other, right or left panel) with the number of items, which type is specified by the first Parameter */class exp_Count : public exp_simpleplaceholder { static const exp_Count instance; exp_Count();public: SIMPLE_EXP_FUNC;};/** * expands %_Filter% ('_' is replaced by 'a', 'o', 'r' or 'l' to indicate the active, other, right or left panel) with the correspondend filter (ie: "*.cpp") */class exp_Filter : public exp_simpleplaceholder { static const exp_Filter instance; exp_Filter();public: SIMPLE_EXP_FUNC;};/** * expands %_Current% ('_' is replaced by 'a', 'o', 'r' or 'l' to indicate the active, other, right or left panel) with the current item ( != the selected onec) */class exp_Current : public exp_simpleplaceholder { static const exp_Current instance; exp_Current();public: SIMPLE_EXP_FUNC;};/** * expands %_List% ('_' is replaced by 'a', 'o', 'r' or 'l' to indicate the active, other, right or left panel) with a list of items, which type is specified by the first Parameter */class exp_List : public exp_simpleplaceholder { static const exp_List instance; exp_List();public: SIMPLE_EXP_FUNC;};/** * expands %_ListFile% ('_' is replaced by 'a', 'o', 'r' or 'l' to indicate the active, other, right or left panel) with the name of a temporary file, containing a list of items, which type is specified by the first Parameter */class exp_ListFile : public exp_simpleplaceholder { static const exp_ListFile instance; exp_ListFile();public: SIMPLE_EXP_FUNC;}; /** * expands %_Ask% ('_' is nessesary because there is no panel needed) with the return of an input-dialog */class exp_Ask : public exp_simpleplaceholder { static const exp_Ask instance; exp_Ask();public: SIMPLE_EXP_FUNC;}; /** * This copies it's first Parameter to the clipboard */class exp_Clipboard : public exp_placeholder { static const exp_Clipboard instance; exp_Clipboard();public: EXP_FUNC;}; /** * This selects all items by the mask given with the first Parameter */class exp_Select : public exp_simpleplaceholder { static const exp_Select instance; exp_Select();public: SIMPLE_EXP_FUNC;}; /** * This changes the panel'spath to the value given with the first Parameter. */class exp_Goto : public exp_simpleplaceholder { static const exp_Goto instance; exp_Goto();public: SIMPLE_EXP_FUNC;};/** * This is equal to 'cp <first Parameter> <second Parameter>'. */class exp_Copy : public exp_placeholder { static const exp_Copy instance; exp_Copy();public: EXP_FUNC;};/** * This is equal to 'mv <first Parameter> <second Parameter>'. */class exp_Move : public exp_placeholder { static const exp_Move instance; exp_Move();public: EXP_FUNC;}; /** * This opens the synchronizer with a given profile */class exp_Sync : public exp_simpleplaceholder { static const exp_Sync instance; exp_Sync();public: SIMPLE_EXP_FUNC;};/** * This opens the searchmodule with a given profile */class exp_NewSearch : public exp_simpleplaceholder { static const exp_NewSearch instance; exp_NewSearch();public: SIMPLE_EXP_FUNC;};/** * This loads the panel-profile with a given name */class exp_Profile : public exp_simpleplaceholder { static const exp_Profile instance; exp_Profile();public: SIMPLE_EXP_FUNC;};/** * This is setting marks in the string where he is later splitted up for each {all, selected, files, dirs} */class exp_Each : public exp_simpleplaceholder { static const exp_Each instance; exp_Each();public: SIMPLE_EXP_FUNC;};/** * This sets the sorting on a specific colunm */class exp_ColSort : public exp_simpleplaceholder { static const exp_ColSort instance; exp_ColSort();public: SIMPLE_EXP_FUNC;};/** * This sets relation between the left and right panel */class exp_PanelSize : public exp_simpleplaceholder { static const exp_PanelSize instance; exp_PanelSize();public: SIMPLE_EXP_FUNC;};#ifdef __KJSEMBED__/** * This sets relation between the left and right panel */class exp_Script : public exp_simpleplaceholder { static const exp_Script instance; exp_Script();public: SIMPLE_EXP_FUNC;};const exp_Script exp_Script::instance;#endif/** * This loads a file in the internal viewer */class exp_View : public exp_simpleplaceholder { static const exp_View instance; exp_View();public: SIMPLE_EXP_FUNC;};const exp_View exp_View::instance;const exp_PanelSize exp_PanelSize::instance;const exp_ColSort exp_ColSort::instance;const exp_Each exp_Each::instance;const exp_Profile exp_Profile::instance;const exp_NewSearch exp_NewSearch::instance;const exp_Sync exp_Sync::instance;const exp_Move exp_Move::instance;const exp_Copy exp_Copy::instance;const exp_Goto exp_Goto::instance;const exp_Select exp_Select::instance;const exp_Clipboard exp_Clipboard::instance;const exp_Ask exp_Ask::instance;const exp_ListFile exp_ListFile::instance;const exp_List exp_List::instance;const exp_Current exp_Current::instance;const exp_Filter exp_Filter::instance;const exp_Count exp_Count::instance;const exp_Path exp_Path::instance;//////////////////////////////////////////////////////////////////////////////////// utils /////////////////////////////////////////////////////////////////////////////////////** * escapes everything that confuses bash in filenames * @param s String to manipulate * @return escaped string */QString bashquote( QString s ) { /* // we _can_not_ use this function because it _encloses_ the sting in single-quots! // In this case quotes strings could not be concaternated anymore return KrServices::quote(s); */ static const QString evilstuff = "\\\"'`()[]{}!?;$&<>| \t\r\n"; // stuff that should get escaped for ( unsigned int i = 0; i < evilstuff.length(); ++i ) s.replace( evilstuff[ i ], ('\\' + evilstuff[ i ]) ); return s;}QString separateAndQuote(QStringList list,const QString& separator,const bool quote){ if(quote) transform(list.begin(),list.end(),list.begin(),bashquote); return list.join(separator);}//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// expander classes /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////exp_Path::exp_Path() { _expression = "Path"; _description = i18n("Panel's Path..."); _needPanel = true; addParameter( exp_parameter( i18n("Automatically escape spaces"), "__yes", false ) );}TagString exp_Path::expFunc( const ListPanel* panel, const QStringList& parameter, const bool& useUrl, Expander& exp ) const { NEED_PANEL QString result; if ( useUrl ) result = panel->func->files()->vfs_getOrigin().url() + "/"; else result = panel->func->files()->vfs_getOrigin().path() + "/"; if ( parameter[0].lower() == "no" ) // don't escape spaces return TagString(result); else return TagString(bashquote(result));}exp_Count::exp_Count() { _expression = "Count"; _description = i18n("Number of..."); _needPanel = true; addParameter( exp_parameter( i18n("Count:"), "__choose:All;Files;Dirs;Selected", false ) );}TagString exp_Count::expFunc( const ListPanel* panel, const QStringList& parameter, const bool&, Expander& exp ) const { NEED_PANEL int n = -1; if ( parameter[ 0 ].isEmpty() || parameter[ 0 ].lower() == "all" ) n = panel->view->numDirs() + panel->view->numFiles(); else if ( parameter[ 0 ].lower() == "files" ) n = panel->view->numFiles(); else if ( parameter[ 0 ].lower() == "dirs" ) n = panel->view->numDirs(); else if ( parameter[ 0 ].lower() == "selected" ) n = panel->view->numSelected(); else { setError(exp, Error(Error::S_FATAL,Error::C_ARGUMENT,i18n("Expander: Bad argument to Count: %1 is not valid item specifier").arg(parameter[0]) )); return QString::null; } return TagString(QString("%1").arg( n ));}exp_Filter::exp_Filter() { _expression = "Filter"; _description = i18n("Filter Mask (*.h, *.cpp, etc.)");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?