window-basic-main-dropfiles.cpp 8.3 KB

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