window-basic-main-dropfiles.cpp 8.9 KB

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