1
0

window-basic-main-dropfiles.cpp 9.9 KB

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