window-basic-main-dropfiles.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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", "webp", 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. const char *type = nullptr;
  102. std::vector<const char *> types;
  103. QString name;
  104. obs_video_info ovi;
  105. obs_get_video_info(&ovi);
  106. switch (image) {
  107. case DropType_RawText:
  108. obs_data_set_string(settings, "text", data);
  109. #ifdef _WIN32
  110. type = "text_gdiplus";
  111. #else
  112. type = "text_ft2_source";
  113. #endif
  114. break;
  115. case DropType_Text:
  116. #ifdef _WIN32
  117. obs_data_set_bool(settings, "read_from_file", true);
  118. obs_data_set_string(settings, "file", data);
  119. name = QUrl::fromLocalFile(QString(data)).fileName();
  120. type = "text_gdiplus";
  121. #else
  122. obs_data_set_bool(settings, "from_file", true);
  123. obs_data_set_string(settings, "text_file", data);
  124. type = "text_ft2_source";
  125. #endif
  126. break;
  127. case DropType_Image:
  128. obs_data_set_string(settings, "file", data);
  129. name = QUrl::fromLocalFile(QString(data)).fileName();
  130. type = "image_source";
  131. break;
  132. case DropType_Media:
  133. obs_data_set_string(settings, "local_file", data);
  134. name = QUrl::fromLocalFile(QString(data)).fileName();
  135. type = "ffmpeg_source";
  136. break;
  137. case DropType_Html:
  138. obs_data_set_bool(settings, "is_local_file", true);
  139. obs_data_set_string(settings, "local_file", data);
  140. obs_data_set_int(settings, "width", ovi.base_width);
  141. obs_data_set_int(settings, "height", ovi.base_height);
  142. name = QUrl::fromLocalFile(QString(data)).fileName();
  143. types = {"browser_source", "linuxbrowser-source"};
  144. break;
  145. case DropType_Url:
  146. AddDropURL(data, name, settings, ovi);
  147. types = {"browser_source", "linuxbrowser-source"};
  148. break;
  149. }
  150. for (char const *t : types) {
  151. if (obs_source_get_display_name(t)) {
  152. type = t;
  153. break;
  154. }
  155. }
  156. if (type == nullptr || !obs_source_get_display_name(type)) {
  157. return;
  158. }
  159. if (name.isEmpty())
  160. name = obs_source_get_display_name(type);
  161. std::string sourceName = GenerateSourceName(QT_TO_UTF8(name));
  162. OBSSourceAutoRelease source =
  163. obs_source_create(type, sourceName.c_str(), settings, nullptr);
  164. if (source) {
  165. OBSScene scene = main->GetCurrentScene();
  166. const char *sceneName =
  167. obs_source_get_name(obs_scene_get_source(scene));
  168. auto undo = [sceneName, sourceName](const std::string &) {
  169. OBSSourceAutoRelease source =
  170. obs_get_source_by_name(sourceName.c_str());
  171. obs_source_remove(source);
  172. OBSSourceAutoRelease scene =
  173. obs_get_source_by_name(sceneName);
  174. OBSBasic::Get()->SetCurrentScene(scene.Get(), true);
  175. };
  176. auto redo = [sceneName, sourceName,
  177. type](const std::string &data) {
  178. OBSSourceAutoRelease scene =
  179. obs_get_source_by_name(sceneName);
  180. OBSBasic::Get()->SetCurrentScene(scene.Get(), true);
  181. OBSDataAutoRelease settings =
  182. obs_data_create_from_json(data.c_str());
  183. OBSSourceAutoRelease source = obs_source_create(
  184. type, sourceName.c_str(), settings, nullptr);
  185. obs_scene_add(obs_scene_from_source(scene),
  186. source.Get());
  187. };
  188. undo_s.add_action(QTStr("Undo.Add").arg(sourceName.c_str()),
  189. undo, redo, "",
  190. std::string(obs_data_get_json(settings)));
  191. obs_scene_add(scene, source);
  192. }
  193. }
  194. void OBSBasic::dragEnterEvent(QDragEnterEvent *event)
  195. {
  196. // refuse drops of our own widgets
  197. if (event->source() != nullptr) {
  198. event->setDropAction(Qt::IgnoreAction);
  199. return;
  200. }
  201. event->acceptProposedAction();
  202. }
  203. void OBSBasic::dragLeaveEvent(QDragLeaveEvent *event)
  204. {
  205. event->accept();
  206. }
  207. void OBSBasic::dragMoveEvent(QDragMoveEvent *event)
  208. {
  209. event->acceptProposedAction();
  210. }
  211. void OBSBasic::ConfirmDropUrl(const QString &url)
  212. {
  213. if (url.left(7).compare("http://", Qt::CaseInsensitive) == 0 ||
  214. url.left(8).compare("https://", Qt::CaseInsensitive) == 0) {
  215. activateWindow();
  216. QString msg = QTStr("AddUrl.Text");
  217. msg += "\n\n";
  218. msg += QTStr("AddUrl.Text.Url").arg(url);
  219. QMessageBox messageBox(this);
  220. messageBox.setWindowTitle(QTStr("AddUrl.Title"));
  221. messageBox.setText(msg);
  222. QPushButton *yesButton = messageBox.addButton(
  223. QTStr("Yes"), QMessageBox::YesRole);
  224. QPushButton *noButton =
  225. messageBox.addButton(QTStr("No"), QMessageBox::NoRole);
  226. messageBox.setDefaultButton(yesButton);
  227. messageBox.setEscapeButton(noButton);
  228. messageBox.setIcon(QMessageBox::Question);
  229. messageBox.exec();
  230. if (messageBox.clickedButton() == yesButton)
  231. AddDropSource(QT_TO_UTF8(url), DropType_Url);
  232. }
  233. }
  234. void OBSBasic::dropEvent(QDropEvent *event)
  235. {
  236. const QMimeData *mimeData = event->mimeData();
  237. if (mimeData->hasUrls()) {
  238. QList<QUrl> urls = mimeData->urls();
  239. for (int i = 0; i < urls.size(); i++) {
  240. QUrl url = urls[i];
  241. QString file = url.toLocalFile();
  242. QFileInfo fileInfo(file);
  243. if (!fileInfo.exists()) {
  244. ConfirmDropUrl(url.url());
  245. continue;
  246. }
  247. #ifdef _WIN32
  248. if (fileInfo.suffix().compare(
  249. "url", Qt::CaseInsensitive) == 0) {
  250. QString urlTarget = ReadWindowsURLFile(file);
  251. if (!urlTarget.isEmpty()) {
  252. ConfirmDropUrl(urlTarget);
  253. }
  254. continue;
  255. } else if (fileInfo.isShortcut()) {
  256. file = fileInfo.symLinkTarget();
  257. fileInfo = QFileInfo(file);
  258. if (!fileInfo.exists()) {
  259. continue;
  260. }
  261. }
  262. #endif
  263. QString suffixQStr = fileInfo.suffix();
  264. QByteArray suffixArray = suffixQStr.toUtf8();
  265. const char *suffix = suffixArray.constData();
  266. bool found = false;
  267. const char **cmp;
  268. #define CHECK_SUFFIX(extensions, type) \
  269. cmp = extensions; \
  270. while (*cmp) { \
  271. if (astrcmpi(*cmp, suffix) == 0) { \
  272. AddDropSource(QT_TO_UTF8(file), type); \
  273. found = true; \
  274. break; \
  275. } \
  276. \
  277. cmp++; \
  278. } \
  279. \
  280. if (found) \
  281. continue;
  282. CHECK_SUFFIX(textExtensions, DropType_Text);
  283. CHECK_SUFFIX(htmlExtensions, DropType_Html);
  284. CHECK_SUFFIX(imageExtensions, DropType_Image);
  285. CHECK_SUFFIX(mediaExtensions, DropType_Media);
  286. #undef CHECK_SUFFIX
  287. }
  288. } else if (mimeData->hasText()) {
  289. AddDropSource(QT_TO_UTF8(mimeData->text()), DropType_RawText);
  290. }
  291. }