PageRenderTime 77ms CodeModel.GetById 35ms RepoModel.GetById 4ms app.codeStats 0ms

/managefile/fileutils.cpp

http://filesearch.googlecode.com/
C++ | 420 lines | 301 code | 63 blank | 56 comment | 74 complexity | 93cc643e7df20792ad0999c7e10b03e8 MD5 | raw file
  1. #include <QDir>
  2. #include <QTextStream>
  3. #include <QDebug>
  4. #include <QFile>
  5. #include <QDir>
  6. #include <QDesktopServices>
  7. #include <QUrl>
  8. #include <browser/webview.h>
  9. #include <preferences.h>
  10. #include <QAxObject>
  11. #include <QAxWidget>
  12. #include "fileutils.h"
  13. // ??Copy
  14. bool FileUtils::copyDirectoryFiles(const QDir &fromDir, const QDir &toDir, bool coverFileIfExist)
  15. {
  16. QDir sourceDir = fromDir;
  17. QDir targetDir = toDir;
  18. if(!targetDir.exists()){ /**< ??????????????? */
  19. if(!targetDir.mkdir(toDir.absolutePath()))
  20. return false;
  21. }
  22. QFileInfoList fileInfoList = sourceDir.entryInfoList();
  23. foreach(QFileInfo fileInfo, fileInfoList){
  24. if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
  25. continue;
  26. if(fileInfo.isDir()){ /**< ???????????copy */
  27. if(!copyDirectoryFiles(fileInfo.filePath(),
  28. targetDir.filePath(fileInfo.fileName()),
  29. coverFileIfExist))
  30. return false;
  31. }else{ /**< ??????????????????? */
  32. if(coverFileIfExist && targetDir.exists(fileInfo.fileName())){
  33. targetDir.remove(fileInfo.fileName());
  34. }
  35. /// ????copy
  36. if(!QFile::copy(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()))){
  37. return false;
  38. }
  39. }
  40. }
  41. return true;
  42. }
  43. // ?????copy?????
  44. bool FileUtils::copyFileToDir(QString sfromFile, QString toDir, bool coverFileIfExist)
  45. {
  46. QDir *targetDir = new QDir (toDir);
  47. QFileInfo *fileInfo = new QFileInfo(sfromFile);
  48. /** ??????????????? */
  49. if(!targetDir->exists()){
  50. if(!targetDir->mkpath(targetDir->absolutePath()))
  51. return false;
  52. }
  53. /**< ??????????????????? */
  54. if(coverFileIfExist && targetDir->exists(fileInfo->fileName())){
  55. targetDir->remove(fileInfo->fileName());
  56. }
  57. /// ????copy
  58. if(!QFile::copy(fileInfo->absoluteFilePath(), toDir.append(QDir::separator()).append(fileInfo->fileName()))){
  59. return false;
  60. }
  61. return true;
  62. }
  63. // ??Copy????
  64. bool FileUtils::copyDirectory(const QDir &fromDir, const QDir &toDir)
  65. {
  66. QDir sourceDir = fromDir;
  67. QDir targetDir = toDir;
  68. /**< ??????????????? */
  69. if(!targetDir.exists()){
  70. if(!targetDir.mkdir(toDir.absolutePath())){
  71. return false;
  72. }
  73. }
  74. QFileInfoList fileInfoList = sourceDir.entryInfoList();
  75. foreach(QFileInfo fileInfo, fileInfoList){
  76. if(fileInfo.fileName() == "." || fileInfo.fileName() == ".."){
  77. continue;
  78. }
  79. /**< ???????????copy */
  80. if(fileInfo.isDir()){
  81. if(!copyDirectory(fileInfo.filePath(), targetDir.filePath(fileInfo.fileName()))){
  82. return false;
  83. }
  84. }
  85. }
  86. return true;
  87. }
  88. // ?????
  89. bool FileUtils::delDirectory(const QDir &fromDir)
  90. {
  91. QDir sourceDir = fromDir;
  92. /** ??????????????? */
  93. if(!fromDir.exists()){
  94. return true;
  95. }
  96. QFileInfoList fileInfoList = sourceDir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
  97. if(fileInfoList.size() == 0){
  98. sourceDir.rmpath(fromDir.absolutePath());
  99. }
  100. foreach(QFileInfo fileInfo, fileInfoList){
  101. if(fileInfo.fileName() == "." || fileInfo.fileName() == ".."){
  102. continue;
  103. }
  104. /** ???????????copy */
  105. if(fileInfo.isDir()){
  106. if(!delDirectory(fileInfo.filePath())){
  107. return false;
  108. }
  109. }
  110. }
  111. return true;
  112. }
  113. // ????
  114. bool FileUtils::writeFile(QString filepath, QStringList lines)
  115. {
  116. QFile file(filepath);
  117. if (!file.open( QIODevice::Append | QIODevice::Text )) {
  118. return false;
  119. }
  120. QTextStream stream( &file );
  121. for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it ){
  122. stream << *it << "\n";
  123. }
  124. file.close();
  125. return true;
  126. }
  127. // ????
  128. QStringList FileUtils::readFile(QString filepath)
  129. {
  130. QStringList lines;
  131. QFile file(filepath);
  132. if (!file.open( QIODevice::ReadOnly | QIODevice::Text )) {
  133. return lines;
  134. }
  135. QTextStream stream( &file );
  136. QString line;
  137. while ( !stream.atEnd() ) {
  138. line = stream.readLine(); // ???“\n”?????
  139. lines << line;
  140. }
  141. file.close();
  142. return lines;
  143. }
  144. // ??????????
  145. QStringList FileUtils::readAllDatFile(QString filepath, QStringList lines)
  146. {
  147. QDir dir(filepath);
  148. if(!dir.exists()){
  149. return QStringList();
  150. }
  151. QFileInfoList fileInfoList = dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
  152. foreach(QFileInfo fileInfo, fileInfoList){
  153. if(fileInfo.isDir()){
  154. QString dir = fileInfo.filePath();
  155. lines.append(readAllDatFile(dir, lines));
  156. } else if(fileInfo.isFile()){
  157. QString dir = fileInfo.filePath();
  158. lines.append(readFile(dir));
  159. }
  160. }
  161. return lines;
  162. }
  163. // ?????
  164. bool FileUtils::removeDirectory(QString dirName)
  165. {
  166. QDir dir(dirName);
  167. QString tmpdir = "";
  168. if(!dir.exists()){
  169. return false;
  170. }
  171. QFileInfoList fileInfoList = dir.entryInfoList();
  172. foreach(QFileInfo fileInfo, fileInfoList){
  173. if(fileInfo.fileName() == "." || fileInfo.fileName() == "..")
  174. continue;
  175. if(fileInfo.isDir()){
  176. tmpdir = dirName + ("/") + fileInfo.fileName();
  177. removeDirectory(tmpdir);
  178. dir.rmdir(fileInfo.fileName()); /**< ????? */
  179. } else if(fileInfo.isFile()){
  180. QFile tmpFile(fileInfo.fileName());
  181. dir.remove(tmpFile.fileName()); /**< ?????? */
  182. }
  183. }
  184. /**< ??????????????????????????? */
  185. dir.cdUp();
  186. if(dir.exists(dirName)){
  187. if(!dir.rmdir(dirName))
  188. return false;
  189. }
  190. return true;
  191. }
  192. // ??????????
  193. void FileUtils::deleteDirectory(QFileInfo fileList){
  194. if(fileList.isDir()){
  195. int childCount =0;
  196. QString dir = fileList.filePath();
  197. QDir thisDir(dir);
  198. childCount = thisDir.entryInfoList().count();
  199. QFileInfoList newFileList = thisDir.entryInfoList();
  200. if(childCount >2 ){
  201. for(int i=0; i < childCount; i++){
  202. if(newFileList.at(i).fileName().operator ==(".")|newFileList.at(i).fileName().operator ==("..")){
  203. continue;
  204. }
  205. deleteDirectory(newFileList.at(i));
  206. }
  207. }
  208. fileList.absoluteDir().rmdir(fileList.fileName());
  209. } else if(fileList.isFile()){
  210. fileList.absoluteDir().remove(fileList.fileName());
  211. }
  212. }
  213. // ???????????
  214. int FileUtils::loadAllFile(QDir dir, QList<QString> fileList){
  215. //??
  216. if (!dir.exists()) {
  217. return -1;
  218. }
  219. // ????????????????.?..???????QT?????
  220. dir.setFilter(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot);
  221. //?????
  222. dir.setSorting(QDir::DirsFirst);
  223. //?????list
  224. QFileInfoList list = dir.entryInfoList();
  225. if(list.size()< 1 ) {
  226. return -1;
  227. }
  228. int i=0;
  229. //?????????
  230. do{
  231. QFileInfo fileInfo = list.at(i);
  232. //?????????
  233. bool bisDir = fileInfo.isDir();
  234. if(bisDir) {
  235. loadAllFile(QDir(fileInfo.filePath()), fileList);
  236. } else{
  237. fileList.append(fileInfo.filePath());
  238. }//end else
  239. i++;
  240. } while(i < list.size());
  241. }
  242. // ????
  243. int FileUtils::openFile(const QString &filepath){
  244. // ????
  245. QDir file(filepath);
  246. if (filepath.isEmpty() || !file.exists()) {
  247. return -1;
  248. }
  249. // QDesktopServices::openUrl(QUrl("file:////Users/Biao/Desktop/1.mp4"));
  250. QDesktopServices::openUrl(QUrl::fromLocalFile(filepath));
  251. }
  252. // webView??????
  253. int FileUtils::openTxtFile(const QString &filepath, WebView &webview){
  254. // ????
  255. QFile file(filepath);
  256. if(!file.exists()){
  257. return -1;
  258. }
  259. QTextStream te(&file);
  260. webview.setHtml(te.readAll());
  261. }
  262. // webView?????Excel??
  263. int FileUtils::readExcelFile(const QString &filepath){
  264. // ?????
  265. QFile file(filepath);
  266. if(!file.exists()){
  267. return -1;
  268. }
  269. QAxObject* excel = new QAxObject( "Excel.Application", 0 );
  270. QAxObject* workbooks = excel->querySubObject( "Workbooks" );
  271. QAxObject* workbook = workbooks->querySubObject( "Open(const QString&)", filepath);
  272. QAxObject* sheets = workbook->querySubObject( "Worksheets" );
  273. // excel.querySubObject("ActiveWorkBook");
  274. QList<QVariantList> data; //Data list from excel, each QVariantList is worksheet row
  275. //worksheets count
  276. int count = sheets->dynamicCall("Count()").toInt();
  277. for (int i=1; i<=count; i++) //cycle through sheets
  278. {
  279. //sheet pointer
  280. QAxObject* sheet = sheets->querySubObject( "Item( int )", i );
  281. QAxObject* rows = sheet->querySubObject( "Rows" );
  282. int rowCount = rows->dynamicCall( "Count()" ).toInt(); //unfortunately, always returns 255, so you have to check somehow validity of cell values
  283. QAxObject* columns = sheet->querySubObject( "Columns" );
  284. int columnCount = columns->dynamicCall( "Count()" ).toInt(); //similarly, always returns 65535
  285. //One of possible ways to get column count
  286. int currentColumnCount = 0;
  287. for (int col=1; col<columnCount; col++)
  288. {
  289. QAxObject* cell = sheet->querySubObject( "Cells( int, int )", currentColumnCount, col );
  290. QVariant value = cell->dynamicCall( "Value()" );
  291. if (value.toString().isEmpty()){
  292. break;
  293. } else{
  294. currentColumnCount = col;
  295. }
  296. }
  297. columnCount = currentColumnCount;
  298. //sheet->dynamicCall( "Calculate()" ); //maybe somewhen it's necessary, but i've found out that cell values are calculated without calling this function. maybe it must be called just to recalculate
  299. for (int row=1; row <= rowCount; row++)
  300. {
  301. QVariantList dataRow;
  302. bool isEmpty = true; //when all the cells of row are empty, it means that file is at end (of course, it maybe not right for different excel files. it's just criteria to calculate somehow row count for my file)
  303. for (int column=1; column <= columnCount; column++)
  304. {
  305. QAxObject* cell = sheet->querySubObject( "Cells( int, int )", row, column );
  306. QVariant value = cell->dynamicCall( "Value()" );
  307. if (!value.toString().isEmpty() && isEmpty)
  308. isEmpty = false;
  309. dataRow.append(value);
  310. }
  311. if (isEmpty) //criteria to get out of cycle
  312. break;
  313. data.append(dataRow);
  314. }
  315. }
  316. workbook->dynamicCall("Close()");
  317. excel->dynamicCall("Quit()");
  318. return 0;
  319. }
  320. // ??Word??
  321. int FileUtils::newWordFile(const QString &filepath){
  322. // ?????
  323. QFile file(filepath);
  324. if(!file.exists()){
  325. return -1;
  326. }
  327. QAxWidget word("Word.Application");
  328. word.setProperty("Visible", true);
  329. QAxObject * documents = word.querySubObject("Documents");
  330. documents->dynamicCall("Add (void)");
  331. QAxObject * document = word.querySubObject("ActiveDocument");
  332. document->dynamicCall("SaveAs (const QString&)", QString(filepath));
  333. document->dynamicCall("Close (boolean)", false);
  334. word.dynamicCall("Quit (void)");
  335. }
  336. //// ??Office??
  337. //QAxWidget FileUtils::openOfficeFileInTab(const QString &filepath){
  338. // // ?????
  339. // QAxWidget wordActive("Word.Application");
  340. // QAxObject* word = wordActive.querySubObject("ActiveDocument");
  341. // // ??preferences
  342. // Preferences* p = Preferences::instance();
  343. // QStringList wordtypes = p->word();
  344. // QString suffixname = suffix(filepath);
  345. // if(wordtypes.contains(suffixname)){
  346. // word->dynamicCall("SetVisible(bool)", true );
  347. // word->querySubObject("Open(const QString&)", QString(filepath));
  348. // return wordActive;
  349. // }
  350. //}
  351. // ???????
  352. QString FileUtils::suffix(const QString &filepath){
  353. // ?????
  354. int dotpos = filepath.lastIndexOf(".");
  355. QString dotsuffix = filepath.right(filepath.length() - dotpos);
  356. return "*" + dotsuffix;
  357. }