window-basic-main-dropfiles.cpp 10 KB

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