FileTreeTableNode.C 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // This may look like C code, but it's really -*- C++ -*-
  2. /*
  3. * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
  4. *
  5. * See the LICENSE file for terms of use.
  6. */
  7. #include "FileTreeTableNode.h"
  8. #include <boost/filesystem/operations.hpp>
  9. #include <boost/filesystem/exception.hpp>
  10. #include <boost/lexical_cast.hpp>
  11. #include <iostream>
  12. #include <time.h>
  13. #include <Wt/WIconPair>
  14. #include <Wt/WStringUtil>
  15. #include <Wt/WText>
  16. using namespace Wt;
  17. FileTreeTableNode::FileTreeTableNode(const boost::filesystem::path& path)
  18. #if BOOST_FILESYSTEM_VERSION < 3
  19. #ifndef WT_NO_STD_WSTRING
  20. : WTreeTableNode(Wt::widen(path.leaf()), createIcon(path)),
  21. #else
  22. : WTreeTableNode(path.leaf(), createIcon(path)),
  23. #endif
  24. #else
  25. : WTreeTableNode(path.leaf().string(), createIcon(path)),
  26. #endif
  27. path_(path)
  28. {
  29. label()->setTextFormat(PlainText);
  30. if (boost::filesystem::exists(path)) {
  31. if (!boost::filesystem::is_directory(path)) {
  32. int fsize = (int)boost::filesystem::file_size(path);
  33. setColumnWidget(1, new WText(boost::lexical_cast<std::string>(fsize)));
  34. columnWidget(1)->setStyleClass("fsize");
  35. } else
  36. setSelectable(false);
  37. std::time_t t = boost::filesystem::last_write_time(path);
  38. struct tm ttm;
  39. #if WIN32
  40. ttm=*localtime(&t);
  41. #else
  42. localtime_r(&t, &ttm);
  43. #endif
  44. char c[100];
  45. strftime(c, 100, "%b %d %Y", &ttm);
  46. setColumnWidget(2, new WText(c));
  47. columnWidget(2)->setStyleClass("date");
  48. }
  49. }
  50. WIconPair *FileTreeTableNode::createIcon(const boost::filesystem::path& path)
  51. {
  52. if (boost::filesystem::exists(path)
  53. && boost::filesystem::is_directory(path))
  54. return new WIconPair("icons/yellow-folder-closed.png",
  55. "icons/yellow-folder-open.png", false);
  56. else
  57. return new WIconPair("icons/document.png",
  58. "icons/yellow-folder-open.png", false);
  59. }
  60. void FileTreeTableNode::populate()
  61. {
  62. if (boost::filesystem::is_directory(path_)) {
  63. std::set<boost::filesystem::path> paths;
  64. boost::filesystem::directory_iterator end_itr;
  65. for (boost::filesystem::directory_iterator i(path_); i != end_itr; ++i)
  66. try {
  67. paths.insert(*i);
  68. } catch (boost::filesystem::filesystem_error& e) {
  69. std::cerr << e.what() << std::endl;
  70. }
  71. for (std::set<boost::filesystem::path>::iterator i = paths.begin();
  72. i != paths.end(); ++i)
  73. try {
  74. addChildNode(new FileTreeTableNode(*i));
  75. } catch (boost::filesystem::filesystem_error& e) {
  76. std::cerr << e.what() << std::endl;
  77. }
  78. }
  79. }
  80. bool FileTreeTableNode::expandable()
  81. {
  82. if (!populated()) {
  83. return boost::filesystem::is_directory(path_);
  84. } else
  85. return WTreeTableNode::expandable();
  86. }