window-basic-main-dropfiles.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include <QDragEnterEvent>
  2. #include <QDragLeaveEvent>
  3. #include <QDragMoveEvent>
  4. #include <QDropEvent>
  5. #include <QFileInfo>
  6. #include <QMimeData>
  7. #include <QUrlQuery>
  8. #include <string>
  9. #include "window-basic-main.hpp"
  10. #include "qt-wrappers.hpp"
  11. using namespace std;
  12. static const char *textExtensions[] = {"txt", "log", nullptr};
  13. static const char *imageExtensions[] = {"bmp", "tga", "png", "jpg",
  14. "jpeg", "gif", nullptr};
  15. static const char *htmlExtensions[] = {"htm", "html", nullptr};
  16. static const char *mediaExtensions[] = {
  17. "3ga", "669", "a52", "aac", "ac3", "adt", "adts", "aif",
  18. "aifc", "aiff", "amb", "amr", "aob", "ape", "au", "awb",
  19. "caf", "dts", "flac", "it", "kar", "m4a", "m4b", "m4p",
  20. "m5p", "mid", "mka", "mlp", "mod", "mpa", "mp1", "mp2",
  21. "mp3", "mpc", "mpga", "mus", "oga", "ogg", "oma", "opus",
  22. "qcp", "ra", "rmi", "s3m", "sid", "spx", "tak", "thd",
  23. "tta", "voc", "vqf", "w64", "wav", "wma", "wv", "xa",
  24. "xm", "3g2", "3gp", "3gp2", "3gpp", "amv", "asf", "avi",
  25. "bik", "crf", "divx", "drc", "dv", "evo", "f4v", "flv",
  26. "gvi", "gxf", "iso", "m1v", "m2v", "m2t", "m2ts", "m4v",
  27. "mkv", "mov", "mp2", "mp2v", "mp4", "mp4v", "mpe", "mpeg",
  28. "mpeg1", "mpeg2", "mpeg4", "mpg", "mpv2", "mts", "mtv", "mxf",
  29. "mxg", "nsv", "nuv", "ogg", "ogm", "ogv", "ogx", "ps",
  30. "rec", "rm", "rmvb", "rpl", "thp", "tod", "ts", "tts",
  31. "txd", "vob", "vro", "webm", "wm", "wmv", "wtv", nullptr};
  32. static string GenerateSourceName(const char *base)
  33. {
  34. string name;
  35. int inc = 0;
  36. for (;; inc++) {
  37. name = base;
  38. if (inc) {
  39. name += " (";
  40. name += to_string(inc + 1);
  41. name += ")";
  42. }
  43. obs_source_t *source = obs_get_source_by_name(name.c_str());
  44. if (!source)
  45. return name;
  46. else
  47. obs_source_release(source);
  48. }
  49. }
  50. void OBSBasic::AddDropURL(const char *url, QString &name, obs_data_t *settings,
  51. const obs_video_info &ovi)
  52. {
  53. QUrl path = QString::fromUtf8(url);
  54. QUrlQuery query = QUrlQuery(path.query(QUrl::FullyEncoded));
  55. int cx = (int)ovi.base_width;
  56. int cy = (int)ovi.base_height;
  57. if (query.hasQueryItem("layer-width"))
  58. cx = query.queryItemValue("layer-width").toInt();
  59. if (query.hasQueryItem("layer-height"))
  60. cy = query.queryItemValue("layer-height").toInt();
  61. obs_data_set_int(settings, "width", cx);
  62. obs_data_set_int(settings, "height", cy);
  63. name = query.hasQueryItem("layer-name")
  64. ? query.queryItemValue("layer-name", QUrl::FullyDecoded)
  65. : path.host();
  66. query.removeQueryItem("layer-width");
  67. query.removeQueryItem("layer-height");
  68. query.removeQueryItem("layer-name");
  69. path.setQuery(query);
  70. obs_data_set_string(settings, "url", QT_TO_UTF8(path.url()));
  71. }
  72. void OBSBasic::AddDropSource(const char *data, DropType image)
  73. {
  74. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  75. obs_data_t *settings = obs_data_create();
  76. obs_source_t *source = nullptr;
  77. const char *type = nullptr;
  78. std::vector<const char *> types;
  79. QString name;
  80. obs_video_info ovi;
  81. obs_get_video_info(&ovi);
  82. switch (image) {
  83. case DropType_RawText:
  84. obs_data_set_string(settings, "text", data);
  85. #ifdef _WIN32
  86. type = "text_gdiplus";
  87. #else
  88. type = "text_ft2_source";
  89. #endif
  90. break;
  91. case DropType_Text:
  92. #ifdef _WIN32
  93. obs_data_set_bool(settings, "read_from_file", true);
  94. obs_data_set_string(settings, "file", data);
  95. name = QUrl::fromLocalFile(QString(data)).fileName();
  96. type = "text_gdiplus";
  97. #else
  98. obs_data_set_bool(settings, "from_file", true);
  99. obs_data_set_string(settings, "text_file", data);
  100. type = "text_ft2_source";
  101. #endif
  102. break;
  103. case DropType_Image:
  104. obs_data_set_string(settings, "file", data);
  105. name = QUrl::fromLocalFile(QString(data)).fileName();
  106. type = "image_source";
  107. break;
  108. case DropType_Media:
  109. obs_data_set_string(settings, "local_file", data);
  110. name = QUrl::fromLocalFile(QString(data)).fileName();
  111. type = "ffmpeg_source";
  112. break;
  113. case DropType_Html:
  114. obs_data_set_bool(settings, "is_local_file", true);
  115. obs_data_set_string(settings, "local_file", data);
  116. obs_data_set_int(settings, "width", ovi.base_width);
  117. obs_data_set_int(settings, "height", ovi.base_height);
  118. name = QUrl::fromLocalFile(QString(data)).fileName();
  119. types = {"browser_source", "linuxbrowser-source"};
  120. break;
  121. case DropType_Url:
  122. AddDropURL(data, name, settings, ovi);
  123. types = {"browser_source", "linuxbrowser-source"};
  124. break;
  125. }
  126. for (char const *t : types) {
  127. if (obs_source_get_display_name(t)) {
  128. type = t;
  129. break;
  130. }
  131. }
  132. if (type == nullptr || !obs_source_get_display_name(type)) {
  133. obs_data_release(settings);
  134. return;
  135. }
  136. if (name.isEmpty())
  137. name = obs_source_get_display_name(type);
  138. source = obs_source_create(type,
  139. GenerateSourceName(QT_TO_UTF8(name)).c_str(),
  140. settings, nullptr);
  141. if (source) {
  142. OBSScene scene = main->GetCurrentScene();
  143. obs_scene_add(scene, source);
  144. obs_source_release(source);
  145. }
  146. obs_data_release(settings);
  147. }
  148. void OBSBasic::dragEnterEvent(QDragEnterEvent *event)
  149. {
  150. // refuse drops of our own widgets
  151. if (event->source() != nullptr) {
  152. event->setDropAction(Qt::IgnoreAction);
  153. return;
  154. }
  155. event->acceptProposedAction();
  156. }
  157. void OBSBasic::dragLeaveEvent(QDragLeaveEvent *event)
  158. {
  159. event->accept();
  160. }
  161. void OBSBasic::dragMoveEvent(QDragMoveEvent *event)
  162. {
  163. event->acceptProposedAction();
  164. }
  165. void OBSBasic::ConfirmDropUrl(const QString &url)
  166. {
  167. if (url.left(7).compare("http://", Qt::CaseInsensitive) == 0 ||
  168. url.left(8).compare("https://", Qt::CaseInsensitive) == 0) {
  169. activateWindow();
  170. QString msg = QTStr("AddUrl.Text");
  171. msg += "\n\n";
  172. msg += QTStr("AddUrl.Text.Url").arg(url);
  173. QMessageBox messageBox(this);
  174. messageBox.setWindowTitle(QTStr("AddUrl.Title"));
  175. messageBox.setText(msg);
  176. QPushButton *yesButton = messageBox.addButton(
  177. QTStr("Yes"), QMessageBox::YesRole);
  178. QPushButton *noButton =
  179. messageBox.addButton(QTStr("No"), QMessageBox::NoRole);
  180. messageBox.setDefaultButton(yesButton);
  181. messageBox.setEscapeButton(noButton);
  182. messageBox.setIcon(QMessageBox::Question);
  183. messageBox.exec();
  184. if (messageBox.clickedButton() == yesButton)
  185. AddDropSource(QT_TO_UTF8(url), DropType_Url);
  186. }
  187. }
  188. void OBSBasic::dropEvent(QDropEvent *event)
  189. {
  190. const QMimeData *mimeData = event->mimeData();
  191. if (mimeData->hasUrls()) {
  192. QList<QUrl> urls = mimeData->urls();
  193. for (int i = 0; i < urls.size(); i++) {
  194. QUrl url = urls[i];
  195. QString file = url.toLocalFile();
  196. QFileInfo fileInfo(file);
  197. if (!fileInfo.exists()) {
  198. ConfirmDropUrl(url.url());
  199. continue;
  200. }
  201. QString suffixQStr = fileInfo.suffix();
  202. QByteArray suffixArray = suffixQStr.toUtf8();
  203. const char *suffix = suffixArray.constData();
  204. bool found = false;
  205. const char **cmp;
  206. #define CHECK_SUFFIX(extensions, type) \
  207. cmp = extensions; \
  208. while (*cmp) { \
  209. if (strcmp(*cmp, suffix) == 0) { \
  210. AddDropSource(QT_TO_UTF8(file), type); \
  211. found = true; \
  212. break; \
  213. } \
  214. \
  215. cmp++; \
  216. } \
  217. \
  218. if (found) \
  219. continue;
  220. CHECK_SUFFIX(textExtensions, DropType_Text);
  221. CHECK_SUFFIX(htmlExtensions, DropType_Html);
  222. CHECK_SUFFIX(imageExtensions, DropType_Image);
  223. CHECK_SUFFIX(mediaExtensions, DropType_Media);
  224. #undef CHECK_SUFFIX
  225. }
  226. } else if (mimeData->hasText()) {
  227. AddDropSource(QT_TO_UTF8(mimeData->text()), DropType_RawText);
  228. }
  229. }