window-basic-main-dropfiles.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. if (query.hasQueryItem("layer-css")) {
  62. // QUrl::FullyDecoded does NOT properly decode a
  63. // application/x-www-form-urlencoded space represented as '+'
  64. // Thus, this is manually filtered out before QUrl's
  65. // decoding kicks in again. This is to allow JavaScript's
  66. // default searchParams.append function to simply append css
  67. // to the query parameters, which is the intended usecase for this.
  68. QString fullyEncoded =
  69. query.queryItemValue("layer-css", QUrl::FullyEncoded);
  70. fullyEncoded = fullyEncoded.replace("+", "%20");
  71. QString decoded = QUrl::fromPercentEncoding(
  72. QByteArray::fromStdString(QT_TO_UTF8(fullyEncoded)));
  73. obs_data_set_string(settings, "css", QT_TO_UTF8(decoded));
  74. }
  75. obs_data_set_int(settings, "width", cx);
  76. obs_data_set_int(settings, "height", cy);
  77. name = query.hasQueryItem("layer-name")
  78. ? query.queryItemValue("layer-name", QUrl::FullyDecoded)
  79. : path.host();
  80. query.removeQueryItem("layer-width");
  81. query.removeQueryItem("layer-height");
  82. query.removeQueryItem("layer-name");
  83. query.removeQueryItem("layer-css");
  84. path.setQuery(query);
  85. obs_data_set_string(settings, "url", QT_TO_UTF8(path.url()));
  86. }
  87. void OBSBasic::AddDropSource(const char *data, DropType image)
  88. {
  89. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  90. obs_data_t *settings = obs_data_create();
  91. obs_source_t *source = nullptr;
  92. const char *type = nullptr;
  93. std::vector<const char *> types;
  94. QString name;
  95. obs_video_info ovi;
  96. obs_get_video_info(&ovi);
  97. switch (image) {
  98. case DropType_RawText:
  99. obs_data_set_string(settings, "text", data);
  100. #ifdef _WIN32
  101. type = "text_gdiplus";
  102. #else
  103. type = "text_ft2_source";
  104. #endif
  105. break;
  106. case DropType_Text:
  107. #ifdef _WIN32
  108. obs_data_set_bool(settings, "read_from_file", true);
  109. obs_data_set_string(settings, "file", data);
  110. name = QUrl::fromLocalFile(QString(data)).fileName();
  111. type = "text_gdiplus";
  112. #else
  113. obs_data_set_bool(settings, "from_file", true);
  114. obs_data_set_string(settings, "text_file", data);
  115. type = "text_ft2_source";
  116. #endif
  117. break;
  118. case DropType_Image:
  119. obs_data_set_string(settings, "file", data);
  120. name = QUrl::fromLocalFile(QString(data)).fileName();
  121. type = "image_source";
  122. break;
  123. case DropType_Media:
  124. obs_data_set_string(settings, "local_file", data);
  125. name = QUrl::fromLocalFile(QString(data)).fileName();
  126. type = "ffmpeg_source";
  127. break;
  128. case DropType_Html:
  129. obs_data_set_bool(settings, "is_local_file", true);
  130. obs_data_set_string(settings, "local_file", data);
  131. obs_data_set_int(settings, "width", ovi.base_width);
  132. obs_data_set_int(settings, "height", ovi.base_height);
  133. name = QUrl::fromLocalFile(QString(data)).fileName();
  134. types = {"browser_source", "linuxbrowser-source"};
  135. break;
  136. case DropType_Url:
  137. AddDropURL(data, name, settings, ovi);
  138. types = {"browser_source", "linuxbrowser-source"};
  139. break;
  140. }
  141. for (char const *t : types) {
  142. if (obs_source_get_display_name(t)) {
  143. type = t;
  144. break;
  145. }
  146. }
  147. if (type == nullptr || !obs_source_get_display_name(type)) {
  148. obs_data_release(settings);
  149. return;
  150. }
  151. if (name.isEmpty())
  152. name = obs_source_get_display_name(type);
  153. source = obs_source_create(type,
  154. GenerateSourceName(QT_TO_UTF8(name)).c_str(),
  155. settings, nullptr);
  156. if (source) {
  157. OBSScene scene = main->GetCurrentScene();
  158. obs_scene_add(scene, source);
  159. obs_source_release(source);
  160. }
  161. obs_data_release(settings);
  162. }
  163. void OBSBasic::dragEnterEvent(QDragEnterEvent *event)
  164. {
  165. // refuse drops of our own widgets
  166. if (event->source() != nullptr) {
  167. event->setDropAction(Qt::IgnoreAction);
  168. return;
  169. }
  170. event->acceptProposedAction();
  171. }
  172. void OBSBasic::dragLeaveEvent(QDragLeaveEvent *event)
  173. {
  174. event->accept();
  175. }
  176. void OBSBasic::dragMoveEvent(QDragMoveEvent *event)
  177. {
  178. event->acceptProposedAction();
  179. }
  180. void OBSBasic::ConfirmDropUrl(const QString &url)
  181. {
  182. if (url.left(7).compare("http://", Qt::CaseInsensitive) == 0 ||
  183. url.left(8).compare("https://", Qt::CaseInsensitive) == 0) {
  184. activateWindow();
  185. QString msg = QTStr("AddUrl.Text");
  186. msg += "\n\n";
  187. msg += QTStr("AddUrl.Text.Url").arg(url);
  188. QMessageBox messageBox(this);
  189. messageBox.setWindowTitle(QTStr("AddUrl.Title"));
  190. messageBox.setText(msg);
  191. QPushButton *yesButton = messageBox.addButton(
  192. QTStr("Yes"), QMessageBox::YesRole);
  193. QPushButton *noButton =
  194. messageBox.addButton(QTStr("No"), QMessageBox::NoRole);
  195. messageBox.setDefaultButton(yesButton);
  196. messageBox.setEscapeButton(noButton);
  197. messageBox.setIcon(QMessageBox::Question);
  198. messageBox.exec();
  199. if (messageBox.clickedButton() == yesButton)
  200. AddDropSource(QT_TO_UTF8(url), DropType_Url);
  201. }
  202. }
  203. void OBSBasic::dropEvent(QDropEvent *event)
  204. {
  205. const QMimeData *mimeData = event->mimeData();
  206. if (mimeData->hasUrls()) {
  207. QList<QUrl> urls = mimeData->urls();
  208. for (int i = 0; i < urls.size(); i++) {
  209. QUrl url = urls[i];
  210. QString file = url.toLocalFile();
  211. QFileInfo fileInfo(file);
  212. if (!fileInfo.exists()) {
  213. ConfirmDropUrl(url.url());
  214. continue;
  215. }
  216. QString suffixQStr = fileInfo.suffix();
  217. QByteArray suffixArray = suffixQStr.toUtf8();
  218. const char *suffix = suffixArray.constData();
  219. bool found = false;
  220. const char **cmp;
  221. #define CHECK_SUFFIX(extensions, type) \
  222. cmp = extensions; \
  223. while (*cmp) { \
  224. if (astrcmpi(*cmp, suffix) == 0) { \
  225. AddDropSource(QT_TO_UTF8(file), type); \
  226. found = true; \
  227. break; \
  228. } \
  229. \
  230. cmp++; \
  231. } \
  232. \
  233. if (found) \
  234. continue;
  235. CHECK_SUFFIX(textExtensions, DropType_Text);
  236. CHECK_SUFFIX(htmlExtensions, DropType_Html);
  237. CHECK_SUFFIX(imageExtensions, DropType_Image);
  238. CHECK_SUFFIX(mediaExtensions, DropType_Media);
  239. #undef CHECK_SUFFIX
  240. }
  241. } else if (mimeData->hasText()) {
  242. AddDropSource(QT_TO_UTF8(mimeData->text()), DropType_RawText);
  243. }
  244. }