PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/term/FileLocalView.cpp

http://guishell.googlecode.com/
C++ | 111 lines | 91 code | 18 blank | 2 comment | 7 complexity | ea80dd125d360c7588aceb9dfdb86e32 MD5 | raw file
Possible License(s): GPL-2.0
  1. #include "guiterm.hpp"
  2. #include <QDesktopServices>
  3. #include <QUrl>
  4. string cygpathToAbsolute(string filename)
  5. {
  6. QProcess cygpathProcess;
  7. QStringList args;
  8. args.append("-w");
  9. args.append(filename.c_str());
  10. cygpathProcess.start("cygpath", args);
  11. cygpathProcess.waitForFinished();
  12. QByteArray out = cygpathProcess.readAllStandardOutput();
  13. string ret = out.data();
  14. // Replace backslashes with forward slashes
  15. for(int ii=0; ii<ret.length(); ii++)
  16. if(ret[ii] == '\\') ret[ii] = '/';
  17. // Strip trailing newline
  18. if(ret.length() && ret[ret.length()-1]=='\n')
  19. ret = ret.substr(0, ret.length()-1);
  20. return ret;
  21. }
  22. FileLocalView::FileLocalView(RemoteJob *owner, ServerConnection *host, const RPCParams *params)
  23. :JobViewSegment(owner)
  24. {
  25. this->filename = params->getString("file");
  26. setLayout(new QVBoxLayout());
  27. layout()->setContentsMargins(0,0,0,0);
  28. frame = new WidgetFrame(this);
  29. layout()->addWidget(frame);
  30. if(host->isLocal())
  31. {
  32. this->localName = filename;
  33. #ifdef __MINGW32__
  34. localName = cygpathToAbsolute(localName);
  35. #endif
  36. viewFile(localName);
  37. }
  38. else
  39. {
  40. frameLayout = new QVBoxLayout();
  41. frameLayout->setSpacing(0);
  42. frame->setLayout(frameLayout);
  43. downloadProgressLine = new QHBoxLayout();
  44. downloadProgressLine->setSpacing(0);
  45. downloadProgressLine->setContentsMargins(0,0,0,0);
  46. downloadLabel = new QLabel(filename.c_str());
  47. downloadProgressLine->addWidget(downloadLabel);
  48. downloadProgressBar = new QProgressBar();
  49. downloadProgressLine->addWidget(downloadProgressBar);
  50. downloadProgressBar->setMaximum(FILETRANSFER_PROGRESS_MAX);
  51. frameLayout->addLayout(downloadProgressLine);
  52. string basename = Path(filename.c_str()).basename();
  53. this->localName = TempFileManager::instance()->getTempFile(basename);
  54. FileTransfer *download = new FileTransfer(host, filename);
  55. connect(download, SIGNAL(updateProgress(string,double)), this, SLOT(updateProgress(string,double)));
  56. connect(download, SIGNAL(fileFinished(string,string)), this, SLOT(downloadFinished(string,string)));
  57. connect(download, SIGNAL(fileFailed(string,string)), this, SLOT(downloadFailed(string,string)));
  58. download->begin(this->localName);
  59. }
  60. }
  61. FileLocalView::~FileLocalView()
  62. {
  63. delete frame;
  64. }
  65. void FileLocalView::updateProgress(string filename, double progress)
  66. {
  67. downloadProgressBar->setValue(int(progress * FILETRANSFER_PROGRESS_MAX));
  68. }
  69. void FileLocalView::downloadFinished(string remoteFilename, string localFilename)
  70. {
  71. viewFile(localFilename);
  72. }
  73. void FileLocalView::downloadFailed(string filename, string message)
  74. {
  75. job->reportError(message);
  76. }
  77. void FileLocalView::viewFile(string localName)
  78. {
  79. #ifdef __MINGW32__
  80. string url = "file:///"+localName;
  81. #else
  82. string url = "file://"+localName;
  83. #endif
  84. if(!QDesktopServices::openUrl(QUrl(url.c_str(), QUrl::TolerantMode))) {
  85. job->reportError(retprintf("Failed to open %s\n", url.c_str()));
  86. }
  87. }
  88. RPC_HANDLER(localview)
  89. {
  90. RemoteJob *job = getJob(host, params);
  91. JobView *view = job->getView();
  92. view->addSegment(new FileLocalView(job, (ServerConnection*)host, params));
  93. }