Example #1
0
        public UIElement LoadBanner(BannerDefinition banner)
        {
            var baseName  = PathSafe.Combine(_fileCache.CacheDirectory, $"{banner.BundleId}-v{banner.Version}");
            var filename  = baseName + ".zip";
            var directory = baseName + "-extracted";

            if (!DirectoryContainsBanner(directory, banner))
            {
                ExtractZipFile(filename, directory);
            }

            FrameworkElement bannerControl;

            switch (banner.BundleType)
            {
            case BundleType.Xaml:
                bannerControl = LoadXamlBanner(banner, directory);
                break;

            case BundleType.Image:
                bannerControl = LoadImageBanner(banner, directory);
                break;

            default: throw new Exception("Unknown banner type!");
            }

            WpfHelper.RegisterOpen(bannerControl, url => HandleClick(banner, url));
            var loadOnce = new LoadOnceHandler();

            bannerControl.Loaded += (sender, args) => HandleLoaded(banner, loadOnce);

            return(bannerControl);
        }
Example #2
0
        private async Task <bool> FetchBanner(BannerDefinition banner)
        {
            var cacheFilename = $"{banner.BundleId}-v{banner.Version}.zip";

            if (_fileCache.FileAvailable(cacheFilename, true) && IsMd5Valid(banner, cacheFilename))
            {
                return(true);
            }

            try
            {
                using (var stream = await _downloader.OpenReadTaskAsync(banner.DownloadLink))
                {
                    await _fileCache.SaveFileAsync(cacheFilename, stream);

                    return(true);
                }
            }
            catch (Exception ex)
            {
                _fileCache.DeleteFile(cacheFilename);
                _logger.Debug(ex, "Could not fetch banners");
                return(false);
            }
        }
Example #3
0
        public BannerMetric BuildMetric(BannerDefinition banner, BannerMetricType type)
        {
            var productName = _applicationNameProvider.ApplicationNameWithEdition.ToLowerInvariant().Replace(" ", "_");
            var metric      = new BannerMetric(productName, MachineId, _versionHelper.FormatWithThreeDigits(), banner.BundleId, banner.Version, type, banner.Campaign);

            return(metric);
        }
Example #4
0
        private FrameworkElement LoadXamlBanner(BannerDefinition banner, string directory)
        {
            var xamlFile = _directory.EnumerateFiles(directory, "*.xaml", SearchOption.AllDirectories).First();

            using (var s = new FileStream(xamlFile, FileMode.Open))
            {
                return((FrameworkElement)XamlReader.Load(s));
            }
        }
        public BannerMetric BuildMetric(BannerDefinition banner, BannerMetricType type)
        {
            var metric = _usageMetricFactory.CreateMetric <BannerMetric>();

            metric.Activity      = type;
            metric.BundleId      = banner.BundleId;
            metric.Campaign      = banner.Campaign;
            metric.BundleVersion = banner.Version;

            return(metric);
        }
Example #6
0
        private FrameworkElement LoadImageBanner(BannerDefinition banner, string directory)
        {
            var image = _directory
                        .EnumerateFiles(directory, "*", SearchOption.AllDirectories)
                        .First(f => f.EndsWith(".jpg", true, CultureInfo.InvariantCulture) || f.EndsWith(".png", true, CultureInfo.InvariantCulture));

            var bannerControl = new ImageBanner();

            bannerControl.SetData(image, banner.Link);

            return(bannerControl);
        }
Example #7
0
        private FrameworkElement LoadXamlBanner(BannerDefinition banner, string directory)
        {
            var xamlFile = _directory.EnumerateFiles(directory, "*.xaml", SearchOption.AllDirectories).First();

            using (var s = new FileStream(xamlFile, FileMode.Open))
            {
                var context = new ParserContext();
                context.BaseUri = new Uri("file://" + Path.GetDirectoryName(xamlFile) + "/", UriKind.Absolute);

                return((FrameworkElement)XamlReader.Load(s, context));
            }
        }
Example #8
0
        private void HandleLoaded(BannerDefinition banner, LoadOnceHandler loadOnce)
        {
            if (loadOnce.IsLoaded)
            {
                return;
            }

            try
            {
                loadOnce.IsLoaded = true;
                var metric = _bannerMetricFactory.BuildMetric(banner, BannerMetricType.Impression);
                _usageStatisticsSender.SendAsync(metric);
            }
            catch { }
        }
Example #9
0
        private void HandleClick(BannerDefinition banner, string url)
        {
            url = UrlHelper.AddUrlParameters(url, banner.LinkParameters);

            if (!string.IsNullOrWhiteSpace(banner.Campaign))
            {
                url = UrlHelper.AddUrlParameters(url, "cmp", banner.Campaign);
            }

            try
            {
                _processStarter.Start(url);

                var metric = _bannerMetricFactory.BuildMetric(banner, BannerMetricType.Click);
                _usageStatisticsSender.SendAsync(metric);
            }
            catch { }
        }
Example #10
0
        private bool DirectoryContainsBanner(string directory, BannerDefinition banner)
        {
            if (!_directory.Exists(directory))
            {
                return(false);
            }

            var containsXaml   = _directory.EnumerateFiles(directory, "*.xaml", SearchOption.AllDirectories).Any();
            var containsImages = _directory
                                 .EnumerateFiles(directory, "*", SearchOption.AllDirectories)
                                 .Any(f => f.EndsWith(".jpg", true, CultureInfo.InvariantCulture) || f.EndsWith(".png", true, CultureInfo.InvariantCulture));

            switch (banner.BundleType)
            {
            case BundleType.Xaml: return(containsXaml);

            case BundleType.Image: return(containsImages);

            default: throw new Exception("Unknown banner type!");
            }
        }
Example #11
0
        private bool IsMd5Valid(BannerDefinition banner, string filename)
        {
            var fullPath = _fileCache.GetCacheFilePath(filename);

            return(_hashUtil.VerifyFileMd5(fullPath, banner.DownloadMd5));
        }